Download presentation
Presentation is loading. Please wait.
1
Sorting Dr. Yingwu Zhu
2
Sorting Consider list x1, x2, x3, … xn
We seek to arrange the elements of the list in order Ascending or descending Some O(n2) schemes easy to understand and implement inefficient for large data sets
3
Review: Selection Sort O(n^2)
Make passes through a list/sublist On each pass reposition correctly some element. E.g., find the smallest item in the sublist and put it into a right position
4
Simple Selection Sort Algorithm
Array-based Can you implement it? template<typename T> void select_sort(T a[], int n); Q: What’s the computing time T(n)?
5
Simple Selection Sort Algorithm
template <typename T> void select_sort(int a[], int n) { for (int i=0; i < n-1; i++) { int min_pos = i; for (int j = i+1; j < n; j++) { if (a[min_pos] > a[j]) min_pos = j; } if (min_pos != i) { T temp = a[i]; a[i] = a[min_pos]; a[min_pos] = temp; } } T(n) = ?
6
Simple Selection Sort Algorithm
Linked List-based template <typename T> void select_sort(T* head);
7
Simple Selection Sort Algorithm
template <typename T> void selection_sort(T* head) { while (head) { T* ptr = head->next; while (ptr) { if (head->data > ptr->data) { T temp = head->data; head->data = ptr->data; ptr->data = temp; } ptr = ptr->next; } head = head->next; }
8
Simple Selection Sort Algorithm
Linked List-based Why not move the nodes around? If we wanna move the nodes around, what more should we do? What difference from array-based?
9
Review of Exchange Sort
Systematically interchange pairs of elements which are out of order Bubble sort does this Out of order, exchange In order, do not exchange
10
Bubble Sort void bubble_sort(int a[], int n)
Can you write one in C++ function?
11
Bubble Sort void bubble_sort(int a[], int n) {
int num_compares = n-1; //first should do n-1 comparisons while (num_compares > 0) { int last = 0; //why need this? for (int i=0; i<num_compares; i++) { if (a[i] > a[i+1]) { swap(a[i], a[i+1]); last = i; } //end if num_compares = last; } //end while } // thinking: why need last = i??? The purpose of last?
12
Bubble Sort Algorithm What is the worst case for Bubble Sort?
The list of items are in decreasing order. T(n) = O(n^2)
13
Bubble Sort Disadvantage? Advantage over selection sort?
Swap of data items, but if data item is large, swap could be very inefficient Advantage over selection sort? Does not need to pass through the whole list if the list is partially sorted. It can detect it.
14
Insertion Sort Repeatedly insert a new element into an already sorted list Note this works well with a linked list implementation Why not good for arrays? Dynamic sorting algorithm
15
Example of Insertion Sort
Given list to be sorted 67, 33, 21, 84, 49, 50, 75 Note sequence of steps carried out
16
Algorithm for Linear Insertion Sort
Can you write one in C++ function? void insertion_sort(int a[], int n)
17
Algorithm for Linear Insertion Sort
void insertion_sort(int a[], int n) { int temp; for (int i = 1; i<n; i++) { temp = a[i]; // keep the item to be inserted for (int j = i-1; j >= 0; j--) //go through the sorted sublist if (temp < a[j]) a[j+1] = a[j]; //shift the items else break; a[j+1] = temp; } //end for }
18
Algorithm for Linear Insertion Sort
T(n) = ? If we use a linked-list instead of an array, how do we adapt the previous code? Write a linked-list based one Do you know the difference?
19
Can we have better Sorting Algorithms
We seek improved computing times for sorts of large data sets, better than O(n^2) Chapter presents schemes (e.g. heapsort) which can be proven to have average computing time O( n log2n ) Must be said, no such thing as a universally good sorting scheme Results may depend just how out of order list is
20
Comparison of Sorts Algorithm Type of Sort Time (sec) Simple selection
Sort of a randomly generated list of 500 items (Note: times are on 1970s hardware) Algorithm Type of Sort Time (sec) Simple selection Heapsort Bubble sort 2 way bubble sort Quicksort Linear insertion Binary insertion Shell sort Selection Exchange Insertion 69 18 165 141 6 66 37 11
21
Indirect Sorts Possible that the items being sorted are large structures Data transfer/swapping time unacceptable Alternative is indirect sort Uses index table to store positions of the objects Manipulate the index table for ordering Where will this technique be used? Name one
22
Question?
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.