Hi, I've been working on this part of the code for at least 10 hours.
private static void selectionSort( int[] arr ) { for (int stopInd = arr.length - 1; stopInd >= 0; stopInd--) { int positionOfMax = indOfMax(arr,stopInd); for (int i = 0; i <= stopInd; i++) { if (arr[i] < arr[positionOfMax]) positionOfMax = i; } int temp = arr[stopInd]; // swap stopInd item with biggest item arr[stopInd] = arr[positionOfMax ]; arr[positionOfMax] = temp; } }
It finally works but there's a slight problem: The selection sort sorts from larges => smallest.
I.e.
But i want it to be 5, 16, 56, 64 etc.RANDOM:
127 16 175 114 64 5 169 80 53 122 126 82 103 197 195 150 177 161 154 164
SELECTIONSORTED:
197 195 177 175 169 164 161 154 150 127 126 122 114 103 82 80 64 53 16 5
I've switched THE if (arr[i] < arr[positionOfMax]) "<" TO ">" but it doesn't work. instead i would get:
And the program we're writing using cmd line args and I can't find a way to use debugging mode(looking at each step of line of code) on eclipse with cmd line args becaues it just executes at once.RANDOM:
34 119 56 40 83 11 190 168 138 154 63 163 33 141 39 71 99 21 172 18
SELECTIONSORTED:
190 34 119 56 40 83 11 18 168 138 154 63 163 33 141 39 71 99 21 172
Full code
/* Program2.java */ import java.io.*; import java.util.*; public class Program2 { private static final int NOT_FOUND = -1; public static void main( String[] args ) { try // ILL EXPLAIN THIS IN CLASS SOON. JUST LEAVE IT IN HERE SO YOUR PROGRAM CRASH CANT CRASH THE GRADING SCRIPT { Random rand = new Random(); int[] arr; if (args.length < 1 ) { System.out.println("\n\n !! You must enter a desired arr length on the cmd line !!\n"); System.exit(0); } int dimension = Integer.parseInt(args[0]); if ( dimension < 100 && dimension > 10) {// YOU MUST VALIDATE THAT 10 < DIMENSION < 100. JUST EXIT IF INVALID arr = new int[dimension]; randomizearr( arr ); // fill with random ints between 1 .. dimension*10 inclusive printarr( "\nRANDOM:", arr ); bubbleSort( arr ); printarr( "BUBBLESORTED:", arr ); randomizearr( arr ); // fill with random ints between 1 .. dimension inclusive printarr( "RANDOM:", arr ); selectionSort( arr ); printarr( "SELECTIONSORTED:", arr ); } else { System.out.println("Invalid. Program now exits."); } } // LEAVE THIS HERE - DO NOT MODIFY catch (Exception e ) // catch-all Exception is the most general Exception type { System.out.println("EXCEPTION DETECTED\n" + e ); // THIS WILL SHOW UP IN YOUR OUTPUT AND I CAN DETECT IT System.exit(0); } } // END MAIN // ######################################################################################## // - - - - - - - - - Y O U A R E G I V E N T H E S E M E T H O D S. D O N O T M O D I F Y - - - - - - - - - - - // USE THIS METHOD AS GIVEN: DO NOT CHANGE private static void printarr( String label, int[] arr ) { System.out.println(label); for( int i=0 ; i<arr.length ;++i ) System.out.printf("%-3d ",arr[i] ); System.out.println(); } // USE THIS METHOD AS GIVEN: DO NOT CHANGE private static void randomizearr( int[] arr ) { Random r = new Random(); int i=0; while ( i < arr.length ) { int n = r.nextInt(arr.length*10) + 1; // 1 .. dimension*10 inclusive // INSERT ONLY IF NUMBER NOT ALREADY IN arr if ( linearSearch( arr, n ) == -1 ) arr[i++] = n; } } // USE THIS METHOD AS GIVEN: DO NOT CHANGE private static int linearSearch( int[] arr, int target ) { for ( int i=0 ; i < arr.length ; ++ i ) if (arr[i] == target ) return i; return NOT_FOUND; // i.e. return -1 which is never a valid index - means NOT_FOUND } // - - - - - - - - - Y O U W R I T E T H E S E M E T H O D S B E L O W - - - - - - - - - - - // YOU WRITE THIS METHOD // BUBBLESORT: private static void bubbleSort( int[] arr ) { boolean swapped = true; int j = 0; int temp; while (swapped) { swapped = false; j++; for (int i = arr.length-2; i >= 0 ; i--) { if (arr[i] > arr[i + 1]) { temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; swapped = true; } } } // YOUR CODE HERE // for i= index of the next_to_last element DOWNTO 0 // for j = 0 upto i // if arr[j] and arr[j+1] are out of order then swap 'em } // YOU WRITE THIS METHOD // SELECTIONSORT private static void selectionSort( int[] arr ) { for (int stopInd = arr.length - 1; stopInd >= 0; stopInd--) { int positionOfMax = indOfMax(arr,stopInd); for (int i = 0; i <= stopInd; i++) { if (arr[i] < arr[positionOfMax]) positionOfMax = i; } int temp = arr[stopInd]; // swap stopInd item with biggest item arr[stopInd] = arr[positionOfMax ]; arr[positionOfMax] = temp; } } // YOU WRITE THIS METHOD // INDOFMAX // WITH EACH CALL FROm SELECTION SORT YOU WILL BE PASSING IN THE RIGHT SIDE SstopIndPING POINT // OF HOW FAR INTO THE arr TO SCAN // ONLY SCAN THAT SUB-RANGE AND RETURN THE INDEX OF THE LARGEST ELEMENT IN THE SUB-RANGE private static int indOfMax( int[] arr, int stopIndInd ) { // YOUR CODE HERE // for i = 0 upto index of last element // keep track of the largest number you have seen so far and save the index postion of each new winner // // return the index position of the largest numnber seen int max = arr[0]; for(int i = 1; i < arr.length; i++){ if(arr[i] > max) { max = arr[i]; stopIndInd = i; } } return stopIndInd; // JUST TO MAKE IT COMPILE - YOU CHANGE AS NEEDED } } // END Of CLASS PROGRAM2
Help will be appreciated