lecture 6 sorting algorithms: bubble, selection, and insertion

19
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Upload: willis-dawson

Post on 02-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Lecture 6Sorting Algorithms:

Bubble, Selection, and Insertion

Page 2: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Lecture Objectives

• Learn how to implement the simple sorting algorithms (selection, bubble and insertion)

• To learn how to estimate and compare the performance of basic sorting algorithms

• To appreciate that algorithms for the same task can differ widely in performance

Page 3: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Bubble Sort

The idea: •Make repeated passes through a list of items, exchanging adjacent items if necessary.•At each pass, the largest unsorted item will be pushed in its proper place. •Stop when no more exchanges were performed during the last pass.

Page 4: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Bubble Sort1 1 23 2 56 9 8 10 100

2 1 2 23 56 9 8 10 100

3 1 2 23 9 56 8 10 100

4 1 2 23 9 8 56 10 100

5 1 2 23 9 8 10 56 100

---- finish the first traversal ----

1 1 2 23 9 8 10 56 100

2 1 2 9 23 8 10 56 100

3 1 2 9 8 23 10 56 100

4 1 2 9 8 10 23 56 100

---- finish the second traversal ----

Page 5: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Bubble Sort Algorithm

private int[] a = new int[100]; int x;public void sortArray() {

int i; int j;

int temp; for( i = (x - 1); i >= 0; i-- ) { for( j = 1; j <= i; j++ ) {

if( a[ j-1] > a[ j ] ) {temp = a[j-1]; a[j-1] = a[j]; a[j] = temp;

} }

} }

Page 6: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Run time efficiency of bubble sortTwo operations affect the run time efficiency of the bubble sort the most:

the comparison operation in the inner loop, and the exchange operation also in the inner loop.

Therefore, we must say how efficient the bubble sort is. each of the two

operations.

1. Efficiency of the number of comparisons:

during the first iteration of the outer loop, in the inner loop (N - 1 comparisons); during the second iteration of the outer loop: (N - 2 comparisons); ......... during the (N - 1)-th iteration of the outer loop: 1 comparison.

Total number of comparisons: (N - 1) + (N - 2) + ... + 2 + 1 = (N * (N - 1)) / 2

= (N^2 - N) / 2 < N^2

Bubble sort is O(N^2) algorithm. ( the number of comparisons.)

Page 7: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Run time efficiency of bubble sort (cont.)

2. Efficiency of the number of exchanges:

during the first iteration of the outer loop, in the inner loop: at most (N - 1 exchanges);

during the second iteration of the outer loop: at most (N - 2 exchanges); ......... during the (N - 1)-th iteration of the outer loop: at most 1 exchange.

Total number of exchanges: (N - 1) + (N - 2) + ... + 2 + 1 = (N * (N - 1)) / 2 =

(N^2 - N) / 2 < N^2

Bubble sort is O(N^2) algorithm of the number of exchanges.

Note that only one pass through the inner loop is required if the list is already sorted. That is, bubble sort is sensitive to the input, and in the best case (for sorted lists) it is O(N) algorithm of the number of comparisons, and O(1) of the number of exchanges.

Page 8: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Where we use Bubble Sort?

• This algorithm's average and worst case performance is O(n2), so it is rarely used to sort large, unordered, data sets. Bubble sort can be used to sort a small number of items (where its inefficiency is not a high penalty). Bubble sort may also be efficiently used on a list that is already sorted except for a very small number of elements.

Page 9: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Practice (Lab Work)

• Increase the bubble sort algorithm efficiency so that the loop ends if there is no more exchange.

Page 10: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Selection Sort Algorithm• List is sorted by selecting list element and moving it

to its proper position

• Algorithm finds position of smallest element and moves it to top of unsorted portion of list

• Start with the 1st element, scan the entire list to find its smallest element and exchange it with the 1st element

• Start with the 2nd element, scan the remaining list to find the the smallest among the last (N-1) elements and exchange it with the 2nd element

• Repeats process above until entire list is sorted

Page 11: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

11

Selection sort

Page 12: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Selection Sort Algorithm (Cont’d)

public static void selectionSort(int[] list, int listLength) { int index; int smallestIndex; int minIndex; int temp;

for (index = 0; index < listLength – 1; index++) { smallestIndex = index; for (minIndex = index + 1; minIndex < listLength; minIndex++) if (list[minIndex] < list[smallestIndex]) smallestIndex = minIndex;

temp = list[smallestIndex]; list[smallestIndex] = list[index]; list[index] = temp; }}

Page 13: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

•Note that the particular arrangement of values in the array does not affect the amount of work done.

•Even if the array is in sorted order before the call to Selection Sort, the function still makes n(n-1)/2 comparisons.

Selection Sort Runtime

Selection Sort runtime 8

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

Page 14: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Selection Sort Runtime (cont…)

•Best case: Already sorted

Passes: (n - 1)

Comparisons each pass: (n-k) where k pass number.

Number of comparisons:

(n - 1) + (n - 2) + (n - 3) + … + 1 = n(n-1)/2

= n2/2 - n/2

= O(n2)

•Worst case: Same.

•Number of exchanges:

Always (n - 1) (better than Bubble Sort).

9

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

Selection Sort Runtime

Page 15: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Insertion Algorithm

• In each pass of an insertion sort, one or In each pass of an insertion sort, one or more pieces of data are inserted into their more pieces of data are inserted into their correct location in an ordered list. correct location in an ordered list.

• Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and mostly sorted lists, and often is used as part of more sophisticated algorithms.

Page 16: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Data Structures: A Pseudocode Approach with C, Second Edition

16

Straight insertion sort example

Page 17: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

public void InsertionSort() { for (int i = 1; i < size; i++) { int temp = a[i];

// slide elements down to make room for a[i] int j = i; while (j > 0 && a[j - 1] > temp) { a[j] = a[j - 1]; j--; }

a[j] = temp; } }

Insertion Sort code

Insertion Sort code 17

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

Page 18: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

•Best case: Already sorted O(n) comparisons.

•Worst case: O(n2) comparisons.

•Number of exchanges:

Best case: O(n).

Worst case: O(n2).

• In practice, best for small sets ( < 30 items).

•Very efficient on nearly-sorted inputs.

Insertion Sort Runtime

Insertion Sort Runtime 18

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)

Page 19: Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion

Comparing Simple Sorts

Comparing Simple Sorts•We've seen "simple" sorting algorithms, such as:

Bubble sort, Selection sort, and Insertion sort.

•They all use nested loops and perform approximately n2 comparisons.

•They are relatively inefficient.

Comparisons Swaps

BubbleWorst n(n-1)/2 n(n-1)/2

Best n-1 n-1

Selection n(n-1)/2 n-1

InsertionWorst n(n-1)/2 n(n-1)/2

Best n-1 n-1

19

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computer & IT Algorithms and Data Structures: Lecture (7)