Presentation is loading. Please wait.

Presentation is loading. Please wait.

Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.

Similar presentations


Presentation on theme: "Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the."— Presentation transcript:

1 Sorting Arrays

2 Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the elements of an array, a, are of type int and we want to arrange them in ascending order, i.e., we want a[0] to contain the smallest element, a[1] to contain the next smallest element and so on.

3 Sorting by Swapping Values 3611177155193014 3511177156193014 7611173155193014

4 Selection Sort (cont’d) The following is an outline of the algorithm: for (index = 0; index < a.length; index++) {//Place the correct value in a[index]: indexOfNextSmallest = an index of the smallest value among a[index], a[index+1],..., a[a.length-1]; Interchange the values of a[index] and a[indexOfNextSmallest]. //a[0] <= a[1] <=... <= a[index] and these are the //smallest of the original array elements. //The remaining positions contain the rest of //the original array elements. }

5 Selection Sort Code /** Class for sorting an array of base type int from smallest to largest. */ public class SelectionSort { /** Precondition: Every indexed variable of a has a value. Action: Sorts a so that a[0] <= a[1] <=... <= a[a.length - 1]. */ public static void sort(int[] a) { int index, indexOfNextSmallest; for (index = 0; index < a.length - 1; index++) {//Place the correct value in a[index]: indexOfNextSmallest = indexOfSmallest(index, a); interchange(index,indexOfNextSmallest, a); //a[0] <= a[1] <=...<= a[index] and these are the smallest //of the original array elements. The remaining positions //contain the rest of the original array elements. } }

6 Selection Sort Code (cont’d) /** Returns the index of the smallest value among a[startIndex], a[startIndex+1],... a[a.length - 1] */ private static int indexOfSmallest(int startIndex, int[] a) { int min = a[startIndex]; int indexOfMin = startIndex; int index; for (index = startIndex + 1; index < a.length; index++) if (a[index] < min) { min = a[index]; indexOfMin = index; //min is smallest of a[startIndex] through a[index] } return indexOfMin; } /** Precondition: i and j are valid indices for the array a. Postcondition: Values of a[i] and a[j] have been interchanged. */ private static void interchange(int i, int j, int[] a) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; //original value of a[i] } }

7 Selection Sort Code (cont’d) public class SelectionSortDemo { public static void main(String[] args) { int[] b = {7, 5, 11, 2, 16, 4, 18, 14, 12, 30}; System.out.println("Array values before sorting:"); int i; for (i = 0; i < b.length; i++) System.out.print(b[i] + " "); System.out.println( ); SelectionSort.sort(b); System.out.println("Array values after sorting:"); for (i = 0; i < b.length; i++) System.out.print(b[i] + " "); System.out.println( ); } }


Download ppt "Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the."

Similar presentations


Ads by Google