Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration:...

26
Selection Sorting Lecture 21
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    219
  • download

    2

Transcript of Selection Sorting Lecture 21. Selection Sort Given an array of length n, –In first iteration:...

Page 1: 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.

Selection SortingLecture 21

Page 2: 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.

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

Page 3: 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.

Example and analysis of Selection Sort

• The Selection Sort might swap an array element with itself--this is harmless.

7 2 8 5 4

2 7 8 5 4

2 4 8 5 7

2 4 5 8 7

2 4 5 7 8

Page 4: 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.

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 2 8 5 4

Page 5: 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.

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 2 8 5 4

outer = 0min = 0

Page 6: 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.

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 2 8 5 4

outer = 0min = 0

inner = 1

Page 7: 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.

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 2 8 5 4

outer = 0min = 0

inner = 1

list [inner] = 2

list [min] = 7

Page 8: 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.

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 2 8 5 4

outer = 0min = 1

inner = 1

list [inner] = 2

list [min] = 7

Page 9: 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.

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 2 8 5 4

outer = 0min = 1

inner = 2

list [inner] = 2

list [min] = 7

Page 10: 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.

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 2 8 5 4

outer = 0min = 1

inner = 2

list [inner] = 8

list [min] = 2

Page 11: 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.

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 2 8 5 4

outer = 0min = 1

inner = 3

list [inner] = 8

list [min] = 2

Page 12: 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.

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 2 8 5 4

outer = 0min = 1

inner = 3

list [inner] = 5

list [min] = 2

Page 13: 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.

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 2 8 5 4

outer = 0min = 1

inner = 4

list [inner] = 5

list [min] = 2

Page 14: 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.

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 2 8 5 4

outer = 0min = 1

inner = 4

list [inner] = 4

list [min] = 2

Page 15: 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.

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 7 8 5 4

outer = 0min = 1

inner = 4

list [inner] = 4

list [min] = 2

Page 16: 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.

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 7 8 5 4

outer = 1min = 1

Page 17: 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.

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 7 8 5 4

outer = 1min = 1

inner = 2

Page 18: 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.

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 7 8 5 4

outer = 1min = 1

inner = 2

list [inner] = 8

list [min] = 7

Page 19: 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.

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 7 8 5 4

outer = 1min = 1

inner = 3

list [inner] = 8

list [min] = 7

Page 20: 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.

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 7 8 5 4

outer = 1min = 1

inner = 3

list [inner] = 5

list [min] = 7

Page 21: 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.

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 7 8 5 4

outer = 1min = 3

inner = 3

list [inner] = 5

list [min] = 5

Page 22: 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.

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 7 8 5 4

outer = 1min = 3

inner = 4

list [inner] = 5

list [min] = 5

Page 23: 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.

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 7 8 5 4

outer = 1min = 3

inner = 4

list [inner] = 4

list [min] = 5

Page 24: 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.

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 7 8 5 4

outer = 1min = 4

inner = 4

list [inner] = 4

list [min] = 4

Page 25: 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.

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 4 8 5 7

outer = 1min = 4

inner = 4

list [inner] = 4

list [min] = 4

Page 26: 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.

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 4 8 5 7

outer = 2min = 2