Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap.

Similar presentations


Presentation on theme: "Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap."— Presentation transcript:

1 Selection Sorting Lecture 21

2 Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap it with the element in location 0 –In second iteration: Search elements 1 through n-1 and select the smallest Swap it with the element in location 1 –In third iteration: Search elements 2 through n-1 and select the smallest Swap it with the element in location 2 –Continue in this fashion until there’s nothing left to search

3 Example and analysis of Selection Sort The Selection Sort might swap an array element with itself--this is harmless. 7 2854 2 7854 2 4857 2 4587 2 4578

4 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854

5 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 0

6 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 0 inner = 1

7 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 0 inner = 1 list [inner] = 2 list [min] = 7

8 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 1 list [inner] = 2 list [min] = 7

9 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 2 list [inner] = 2 list [min] = 7

10 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 2 list [inner] = 8 list [min] = 2

11 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 3 list [inner] = 8 list [min] = 2

12 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 3 list [inner] = 5 list [min] = 2

13 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 4 list [inner] = 5 list [min] = 2

14 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 7 2854 outer = 0 min = 1 inner = 4 list [inner] = 4 list [min] = 2

15 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 0 min = 1 inner = 4 list [inner] = 4 list [min] = 2

16 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 1

17 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 1 inner = 2

18 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 1 inner = 2 list [inner] = 8 list [min] = 7

19 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 1 inner = 3 list [inner] = 8 list [min] = 7

20 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 1 inner = 3 list [inner] = 5 list [min] = 7

21 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 3 inner = 3 list [inner] = 5 list [min] = 5

22 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 3 inner = 4 list [inner] = 5 list [min] = 5

23 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 3 inner = 4 list [inner] = 4 list [min] = 5

24 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 7854 outer = 1 min = 4 inner = 4 list [inner] = 4 list [min] = 4

25 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 4857 outer = 1 min = 4 inner = 4 list [inner] = 4 list [min] = 4

26 C Code of Selection Sort void selectionSort(int list[], int size) { int outer, inner, min; for (outer = 0; outer < size-1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < size; inner++) { if (list[inner] < list[min]) { min = inner; } } // list[min] is least among list[outer]..list[list.length - 1] int temp = list[outer]; list[outer] = list[min]; list[min] = temp; } } 2 4857 outer = 2 min = 2


Download ppt "Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration: Search elements 0 through n-1 and select the smallest Swap."

Similar presentations


Ads by Google