|
| 1 | +import java.util.Random; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +public class Median_Finding { |
| 5 | + |
| 6 | + public static void main(String[] args){ |
| 7 | + Random high = new Random(); |
| 8 | + int size=0; //Initializing size of the array |
| 9 | + int no_of_runs=0; // for the number of runs |
| 10 | + int kth_smallest=0; // kth smallest element |
| 11 | + |
| 12 | + try { |
| 13 | + //reading the size of array |
| 14 | + Scanner scan_array_size = new Scanner(System.in); // scanner for array size |
| 15 | + Scanner scan_runs = new Scanner(System.in); //scanner for number of runs |
| 16 | + Scanner scan_ith_smallest = new Scanner(System.in); // scanner for array size |
| 17 | + |
| 18 | + System.out.println("Enter the number of elements to sort (using quick sort):"); |
| 19 | + size = scan_array_size.nextInt(); |
| 20 | + |
| 21 | + System.out.println("\nsize how many times you want to run the algorithm??"); |
| 22 | + no_of_runs = scan_runs.nextInt(); |
| 23 | + |
| 24 | + System.out.println("\nsize Please enter the ith smallest element you want to find"); |
| 25 | + kth_smallest = scan_ith_smallest.nextInt(); |
| 26 | + |
| 27 | + float average=0; |
| 28 | + |
| 29 | + |
| 30 | +for (int x = 1; x <= no_of_runs; x++) { |
| 31 | + //generating random numbers |
| 32 | + int[] random_integers = new int[size]; |
| 33 | + |
| 34 | + random_integers[0] = high.nextInt(500000); |
| 35 | + |
| 36 | + for(int nextNumber = 1; nextNumber < random_integers.length; ++nextNumber) { |
| 37 | + random_integers[nextNumber] = high.nextInt(20); |
| 38 | + |
| 39 | + for( int testNumber = 0; testNumber < nextNumber; ++testNumber ) { |
| 40 | + if( random_integers[testNumber] == random_integers[nextNumber] ) { |
| 41 | + random_integers[nextNumber] = high.nextInt(500000); |
| 42 | + testNumber = 0; |
| 43 | + } |
| 44 | + }// end of for, test number |
| 45 | + }// end of for, next number |
| 46 | + |
| 47 | + /** |
| 48 | + // print the array before sorting |
| 49 | + System.out.println("before sorting"); |
| 50 | + PrintArray(random_integers); |
| 51 | + */ |
| 52 | + |
| 53 | + int low = 0; |
| 54 | + int high1 = random_integers.length - 1; |
| 55 | + int smallest; |
| 56 | + long start = System.currentTimeMillis(); // starting timer |
| 57 | + smallest = kthSmallest(random_integers,low,high1,kth_smallest); |
| 58 | + long time = System.currentTimeMillis() - start; //ending timer |
| 59 | + |
| 60 | + // print the array after sorting |
| 61 | + System.out.println("after sorting"); |
| 62 | + PrintArray(random_integers); |
| 63 | + |
| 64 | + System.out.println("\nrun "+ x +" time taken:" + time + " milli second"); |
| 65 | + System.out.println("\nkth smallest element is:"+smallest); |
| 66 | + //average[x]=(int)time; |
| 67 | + average+=(float)time; |
| 68 | +} // end of for loop for number of runs |
| 69 | + System.out.println("\naverage time taken: "+average/no_of_runs+ " milli second" ); |
| 70 | + |
| 71 | + |
| 72 | + } // end of try |
| 73 | + catch (Exception e) { |
| 74 | + System.out.println("\nsize Invalid Input"); |
| 75 | + e.printStackTrace(); |
| 76 | + } // end of catch |
| 77 | + |
| 78 | + } // end of main |
| 79 | + |
| 80 | + |
| 81 | + private static int medianFind(int[] tempArr, int size) { |
| 82 | + int i,j,min,temp; |
| 83 | + for ( i = 0; i < tempArr.length; i++){ |
| 84 | + min=i; |
| 85 | + for(j=i+1; j < tempArr.length; j++){ |
| 86 | + if(tempArr[j]<tempArr[min]) |
| 87 | + min=j; |
| 88 | + } |
| 89 | + // now swap the elements |
| 90 | + temp=tempArr[min]; |
| 91 | + tempArr[min]=tempArr[i]; |
| 92 | + tempArr[i]=temp; |
| 93 | + } |
| 94 | + |
| 95 | + return tempArr[size/2]; |
| 96 | + }// end of medianFind |
| 97 | + |
| 98 | + |
| 99 | + public static int kthSmallest(int[] arr, int low, int high, int k){ |
| 100 | + if (k > 0 && k <= high-low+1){ |
| 101 | + int size = high-low+1; |
| 102 | + int i; |
| 103 | + |
| 104 | + int [] median = new int[(size+4)/5]; |
| 105 | + int [] temp = new int[5]; |
| 106 | + |
| 107 | + for (i=0; i<size/5; i++){ |
| 108 | + for (int j=0; j<5; j++) |
| 109 | + temp[j] = arr[i*5+j]; |
| 110 | + |
| 111 | + median[i] = medianFind(temp, 5); |
| 112 | + |
| 113 | + } |
| 114 | + if (i*5 < size) //For last group with less than 5 elements |
| 115 | + { |
| 116 | + for (int j=0; j < (i*5)%size; j++) |
| 117 | + temp[j] = arr[i*5+j]; |
| 118 | + |
| 119 | + median[i] = medianFind(temp, (i*5)%size); |
| 120 | + |
| 121 | + i++; |
| 122 | + } |
| 123 | + |
| 124 | + int pivot; |
| 125 | + if (i==1) |
| 126 | + pivot = median[i-1]; |
| 127 | + else |
| 128 | + pivot = kthSmallest(median, 0, i-1, i/2); |
| 129 | + |
| 130 | + // Partition the array around a random element and |
| 131 | + // get position of pivot element in sorted array |
| 132 | + int pos = partition(arr, low, high, pivot); |
| 133 | + |
| 134 | + // If position is same as k |
| 135 | + if (pos-low == k-1) |
| 136 | + return arr[pos]; |
| 137 | + if (pos-low > k-1) // If position is more, recur for left |
| 138 | + return kthSmallest(arr, low, pos-1, k); |
| 139 | + |
| 140 | + // Else recur for right subarray |
| 141 | + return kthSmallest(arr, pos+1, high, k-pos+low-1); |
| 142 | + } |
| 143 | + |
| 144 | + // If k is more than number of elements in array |
| 145 | + return 9999999; |
| 146 | + }// end of method ith smallest |
| 147 | + |
| 148 | + static int partition(int arr[], int low, int high, int x) |
| 149 | + { |
| 150 | + // Search for x in arr[low..high] and move it to end |
| 151 | + int i; |
| 152 | + for (i=low; i<high; i++) |
| 153 | + if (arr[i] == x) |
| 154 | + break; |
| 155 | + swap(arr, i, high); |
| 156 | + |
| 157 | + // Standard partition algorithm |
| 158 | + i = low; |
| 159 | + for (int j = low; j <= high - 1; j++) |
| 160 | + { |
| 161 | + if (arr[j] <= x) |
| 162 | + { |
| 163 | + swap(arr, i, j); |
| 164 | + i++; |
| 165 | + } |
| 166 | + } |
| 167 | + swap(arr, i, high); |
| 168 | + return i; |
| 169 | + } |
| 170 | + |
| 171 | + private static void swap (int [] arr, int i, int j){ |
| 172 | + int temp = arr[i]; |
| 173 | + arr [i] = arr [j]; |
| 174 | + arr[j] = temp; |
| 175 | + } |
| 176 | + // this function prints the array |
| 177 | + private static void PrintArray(int[] random_integers) { |
| 178 | + for (int i = 0; i < random_integers.length; i++) |
| 179 | + {System.out.println(random_integers[i]);} |
| 180 | + } |
| 181 | + |
| 182 | + |
| 183 | +}//end of class |
| 184 | + |
| 185 | + |
0 commit comments