22
Sorting Sanghyun Park Fall 2002 CSE, POSTECH

Sorting Sanghyun Park Fall 2002 CSE, POSTECH. Sorts To Consider Selection sort Bubble sort Insertion sort Merge sort Quick sort Why do we care about sorting?

Embed Size (px)

Citation preview

Sorting

Sanghyun Park

Fall 2002

CSE, POSTECH

Sorts To Consider

Selection sort Bubble sort Insertion sort Merge sort Quick sort

Why do we care about sorting?

Selection Sort

One of the most intuitive sorts

Find the largest item andswap it with the item currently in the last position. Repeat this step on the “unsorted” part of the collection.

Selection Sort

initial array : 29 10 14 37 13 after 1st swap : 29 10 14 13 37 after 2nd swap: 13 10 14 29 37 after 3rd swap: 13 10 14 29 37 after 4th swap: 10 13 14 29 37

Selection Sort

void SelectionSort(int a[], int n)

{

// sort the n elements a[0:n-1]

for (int size = n; size > 1; size--) {

int j = Max(a, size);

Swap(a[j], a[size-1]);

}

}

Complexity O(n2)

Bubble Sort

Not a particularly good algorithm

Compare adjacent items and exchange themif they are out of order. Repeat.The largest item will eventually “bubble” to the correct position.

Bubble Sort

Pass 1 Pass 2

29 10 14 37 13 10 14 29 13 37

10 29 14 37 13 10 14 29 13 37

10 14 29 37 13 10 14 29 13 37

10 14 29 37 13 10 14 13 29 37

10 14 29 13 37

Bubble Sort

void BubbleSort(int a[], int n)

{ // Sort a[0:n-1] using bubble sort

for (int size = n; size > 1; size--) Bubble(a, size);

}

void Bubble(int a[], int n)

{ // Bubble largest element in a[0:n-1] to right

for (int i = 0; i < n -1; i++)

if (a[i] > a[i+1]) Swap(a[i], a[i+1]);

}

O(n2) for worst and average cases,O(n) for best case (already sorted)

Insertion Sort

Imagine arranging a hand of cards,where you pick up one card at a time andinsert into its proper position.

For arrays of data,partition the array into a sorted region and an unsorted region.

Insertion Sort

Initial Array: Action:29 10 14 37 13 Copy 1029 29 14 37 13 Shift 2910 29 14 37 13 Insert 10, copy 1410 29 29 37 13 Shift 2910 14 29 37 13 Insert 14, copy 37, Insert

3710 14 29 37 13 Copy 1310 14 14 29 37 Shift 14, 29, 3710 13 14 29 37 Insert 13

Insertion Sort

void InsertionSort(int a[], int n){ // Sort a[0:n-1] using insertion sort

for (int i = 1; i < n; i++) {int t = a[i];Insert(a, i, t);

}}

Insertion Sort

void Insert(int a[], int n, int x){ // insert x into the sorted array a[0:n-1]

int i;for (i = n-1; i >= 0 && x < a[i]; i--)

a[i+1] = a[i];a[i+1] = x;

}

O(n2) for worst and average cases,O(n) for best case (already sorted)

Mergesort

Divide and Conquer … recursive solution. Divide the array in half, sort each half, then merge. Require the use of a temporary array. Complexity O(n log n)

Original array: 8 1 4 3 2

Divide: 8 1 4 3 2

Sort: 1 4 8 2 3

Merge: 1 2 3 4 8

Quicksort

Divide and Conquer … recursive solution. Partition the array by establishing a pivot point. Concatenate the two sorted arrays. O(n log n) for best and average cases and

O(n2) for worst case (sorted, nearly sorted, or reverse sorted)

Summary

Algorithm Worst Case Average Case

Selection sort n2 n2

Bubble sort n2 n2

Insertion sort n2 n2

Merge sort n log n n log n

Quick sort n2 n log n

Summary

Why not always use Mergesort … on paper it’s the most efficient?On the average, Mergesort is not quite as fast as quicksort. Mergesort also requires extra storage equal to the size of the array to be sorted.

Why bother with selection sort, bubble sort, and insertion sort?They can suffix for smaller data sets.

Final Exam

Location: 공학 2 동 108 호 Date & time: 12/13 ( 금요일 ) 7 pm – 8:30 pm

(key 를 미리 받아야 함 )

Coverage

Priority Queues– definitions of priority queues,

max (min) tree, max (min) heap– heap operations and their complexity– heap sort– LPT machine scheduling with a min priority queue

Leftist Trees– definitions of extended binary tree and the function s()– definitions and properties of (height-balanced) leftist

trees– operations (especially meld) of leftist trees

Coverage

Tournament Trees– definition of winner tree– sorting with winner tree– bin packing with winner tree

Binary Search Trees– definition of BST, indexed BST, balanced BST (AVL

tree)– remove operation in BST

get(rank) operation in indexed BSTput and rotation operations in AVL tree

Coverage

Graphs– definitions (digraph, complete graph, connected graph,

…)– graph properties (sum of v degrees, numbers of v and e)– graph representation (adjacency matrix and adjacency

list)– search methods (BFS and DFS, study with an example)

Greedy Method– definition– Machine scheduling, container loading, and 0/1

knapsack problem with greedy method– Dijkstra’s algorithm for ‘single source all destinations’

shortest path problem (study with an example)

Coverage

Minimum Cost Spanning Tree– definition and properties– Kruskal’s method, Prim’s method, Sollin’s method

Divide and Conquer– principle– merge sort and its complexity– quick sort and its complexity

Coverage

Divide and Conquer– principle– recurrence equation for 0/1 knapsack problem– recurrence equation for other problems

Miscellaneous– sorting algorithms– study homework 4, 5, 6– be creative …