Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bohyung Han CSE, POSTECH

Similar presentations


Presentation on theme: "Bohyung Han CSE, POSTECH"— Presentation transcript:

1 Bohyung Han CSE, POSTECH bhhan@postech.ac.kr
Lecture9: Sorting I CSED233: Data Structures (2014F) Bohyung Han CSE, POSTECH

2 Sorting Important operation when organizing data An example
Ordering of elements Finding duplicate elements Ranking elements (i.e., nth largest) etc. An example There are many different strategies and we need to care about Correctness Efficiency (in terms of time complexity) Input: 3 6 1 9 4 2 Output: 1 2 3 4 6 9

3 Sorting Algorithms Bubble sort Selection sort Insertion sort
Merge sort Quick sort

4 Bubble Sort Comparison of neighbors After iteration Repeat loop
Swap if not in order Iterate left to right After iteration The largest element is located at the rightmost spot. Repeat loop No need to revisit the largest element Loop keeps decreasing in length.

5 Bubble Sort 1st iteration 2nd iteration 3rd iteration 5th iteration 3
6 1 9 4 2 3 1 6 4 2 9 1 3 4 2 6 9 1 2 3 4 6 9 3 6 1 9 4 2 1 3 6 4 2 9 1 3 4 2 6 9 Final result 3 1 6 9 4 2 1 3 6 4 2 9 1 3 4 2 6 9 1 2 3 4 6 9 3 1 6 9 4 2 1 3 6 4 2 9 1 3 2 4 6 9 3 1 6 9 4 2 1 3 4 6 2 9 Compare 4th iteration 3 1 6 4 9 2 1 3 4 6 2 9 1 3 2 4 6 9 Swap 3 1 6 4 9 2 1 3 4 2 6 9 1 3 2 4 6 9 3 1 6 4 2 9 1 2 3 4 6 9

6 Bubble Sort Algorithm public void bubble_sort(int[] values) {
// Check for empty or null array if (values == null || values.length == 0) return; for (int i=0; i<values.length‐1; i++) for (int j=0; j<values.length‐i‐1; j++) if (values[j] > values[j+1]) swap(values, j, j+1); } public void swap(int[] arr, int i, int j) int t = arr[i]; arr[i] = arr[j]; arr[j] = t;

7 Analysis of Bubble Sort
Number of comparisons and swaps in the worst case Time complexity Best case: 𝑂 𝑛 Worst case: 𝑂 𝑛 2 Average case: 𝑂 𝑛 2 𝑛−1 + 𝑛−2 +⋯+2+1= 𝑛 𝑛−1 2 ~ 𝑂 𝑛 2

8 Selection Sort Problem with bubble sort: too many swaps
Number of comparisons: 𝑂 𝑛 2 Number of swaps: 𝑂 𝑛 2 Costly if swap operation takes time Better approach: one swap per iteration Identify element to swap through comparisons Swap the smallest element with the one in the left‐most position. Iterative growth of “sorted area” in array

9 Selection Sort 1st iteration 3rd iteration 5th iteration 3 6 1 9 4 2 1
min min min 1 6 3 9 4 2 1 2 3 4 6 9 4th iteration Swap Swap 1 2 3 9 4 6 2nd iteration min 1 6 3 9 4 2 Final result 1 2 3 4 9 6 1 2 3 4 6 9 min Swap 1 2 3 9 4 6 Swap

10 Selection Sort Algorithm
public void selection_sort(int[] values) { // Check for empty or null array if (values == null || values.length == 0) return; for (int i=0; i<values.length‐1; i++) int minIndex = i; for (int j=i+1; j<values.length; j++) if (values[j] < values[minIndex]) minIndex = j; } swap(values, i, minIndex);

11 Analysis of Selection Sort
Time complexity Best case: 𝑂 𝑛 2 comparisons Worst case: 𝑂 𝑛 2 comparisons Average case: 𝑂 𝑛 2 comparisons Much less swaps: 𝑂 𝑛

12 Insertion Sort Slightly better than selection sort Idea:
Insert next element into partially sorted array Iterate Insertion requires shifting of elements.

13 Insertion Sort 1st iteration 3rd iteration 5th iteration 3 6 1 9 4 2 1
2nd iteration 4th iteration 3 6 1 9 4 2 1 3 6 9 4 2 Insert 1 3 6 9 4 2 1 3 4 6 9 2 Final result Insert Insert 1 2 3 4 6 9

14 Insertion Sort Algorithm
public void insertion_sort(int[] values) { // Check for empty or null array if (values == null || values.length == 0) return; for (int i=1; i<values.length; i++) int temp = values[i]; int j = i; while (j>0 && values[j‐1]>temp) values[j] = values[j‐1]; j‐‐; } values[j] = temp;

15 Analysis of Insertion Sort
Time complexity Best case: 𝑂 𝑛 comparisons and swaps Worst case: 𝑂 𝑛 2 comparisons and swaps Average case: 𝑂 𝑛 2 comparisons and swaps

16 Comparisons of Sorting Algorithms
The three algorithms have the same time complexity but their empirical speeds are different. Bubble sort Selection sort Sorting time in milliseconds Insertion sort Size of array (n)

17


Download ppt "Bohyung Han CSE, POSTECH"

Similar presentations


Ads by Google