Arrays and Sorting
Process <ul><li>Open a file that contains integers, one per line. </li></ul><ul><li>Read each line, convert to  short  and...
Text file containing integers Array of short (integers) numbers.txt numbers[]
Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 0 9 0 8 0 7 0 6 0 5 19 4 3 3...
Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 inputFromFile(inputFileName,...
private static int inputFromFile(String filename, short[] numbers){   TextFileInput in = new TextFileInput(filename);    i...
Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 19 3 12 subArrayLength is 5 0 9 0 8 ...
numbers[10] subArrayLength 5 Partially-filled array for (int i =0;i<numbers.length; i ++) { for (int i =0;i<subArrayLength...
numbers[10] subArrayLength 5 //  average the numbers sum=0; for (int i =0; i<subArrayLength; i ++) sum += numbers[i]; Aver...
numbers[10] subArrayLength 5 //  find the smallest number smallest = numbers[0]; for (int i =1; i<subArrayLength; i ++) sm...
numbers[10] subArrayLength 5 //  find the  index  of the smallest number indexLowest = 0; for ( int j = 1; j < subArrayLen...
numbers[10] //  find the  index  of the smallest number This is the basis of  “Selection Sort” 0 9 0 8 0 7 0 6 0 5 12 4 3 ...
numbers[10] find the  index  of the smallest number This is the basis of  “Selection Sort” Find the smallest number and sw...
numbers[10] numbers[10] sorted Not sorted; Find the smallest number here and swap it with numbers[1] 0 9 0 8 0 7 0 6 0 5 1...
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
private static void selectionSort (short[] array, int length) {  for ( int i = 0; i < length - 1; i++ ) {  int indexLowest...
Upcoming SlideShare
Loading in …5
×

1 arrays and sorting

547 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
547
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

1 arrays and sorting

  1. 1. Arrays and Sorting
  2. 2. Process <ul><li>Open a file that contains integers, one per line. </li></ul><ul><li>Read each line, convert to short and store each into an array </li></ul><ul><li>Sort the array </li></ul><ul><li>Output the sorted array </li></ul>
  3. 3. Text file containing integers Array of short (integers) numbers.txt numbers[]
  4. 4. Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0
  5. 5. Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 12 3 19 inputFromFile(inputFileName, numbersArray); 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0
  6. 6. private static int inputFromFile(String filename, short[] numbers){ TextFileInput in = new TextFileInput(filename); int lengthFilled = 0; String line = in.readLine(); while ( lengthFilled < numbers.length && line != null ) { numbers[lengthFilled++] = Short.parseShort(line); line = in.readLine(); } // while if ( line != null ) { System.out.println(&quot;File contains too many numbers.&quot;); System.out.println(&quot;This program can process only &quot; + numbers.length + &quot; numbers.&quot;); System.exit(1); } // if in.close(); return lengthFilled; } // method inputFromFile Main program calls subArrayLength = inputFromFile(inputFileName, numbersArray); Program 1.1
  7. 7. Text file containing integers Array of short (integers) numbers.txt numbers[10] 123 5 19 3 12 subArrayLength is 5 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  8. 8. numbers[10] subArrayLength 5 Partially-filled array for (int i =0;i<numbers.length; i ++) { for (int i =0;i<subArrayLength; i ++) { NO: YES: 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  9. 9. numbers[10] subArrayLength 5 // average the numbers sum=0; for (int i =0; i<subArrayLength; i ++) sum += numbers[i]; Average = sum/subArrayLength; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  10. 10. numbers[10] subArrayLength 5 // find the smallest number smallest = numbers[0]; for (int i =1; i<subArrayLength; i ++) smallest = Math.min(smallest,numbers[i]; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  11. 11. numbers[10] subArrayLength 5 // find the index of the smallest number indexLowest = 0; for ( int j = 1; j < subArrayLength; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j; 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  12. 12. numbers[10] // find the index of the smallest number This is the basis of “Selection Sort” 0 9 0 8 0 7 0 6 0 5 12 4 3 3 19 2 5 1 123 0
  13. 13. numbers[10] find the index of the smallest number This is the basis of “Selection Sort” Find the smallest number and swap it with the number at the top of the array numbers[10] 0 9 0 8 0 7 0 6 0 5 19 4 3 3 12 2 5 1 123 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
  14. 14. numbers[10] numbers[10] sorted Not sorted; Find the smallest number here and swap it with numbers[1] 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
  15. 15. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0
  16. 16. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 12 4 123 3 19 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0
  17. 17. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 19 4 123 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
  18. 18. numbers[10] numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
  19. 19. numbers[10] sorted 0 9 0 8 0 7 0 6 0 5 123 4 19 3 12 2 5 1 3 0
  20. 20. private static void selectionSort (short[] array, int length) { for ( int i = 0; i < length - 1; i++ ) { int indexLowest = i; for ( int j = i + 1; j < length; j++ ) if ( array[j] < array[indexLowest] ) indexLowest = j; if ( array[indexLowest] != array[i] ) { short temp = array[indexLowest]; array[indexLowest] = array[i]; array[i] = temp; } // if } // for i } // method selectionSort

×