lecture10 - Sorting Dr.YingwuZhu Sorting Considerlist...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Sorting Dr. Yingwu Zhu
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Sorting Consider list x 1 , x 2 , x 3 , … x n We seek to arrange the elements of the  list in order Ascending or descending Some O(n 2 ) schemes easy to understand and implement inefficient for large data sets
Background image of page 2
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
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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)?  
Background image of page 4
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) = ?
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Simple Selection Sort Algorithm Linked List-based template <typename T> void select_sort(T* head);
Background image of page 6
Simple Selection Sort Algorithm template <typename T> void selection_sort(T* head) {     while (head) {        T* ptr = head->next;
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 22

lecture10 - Sorting Dr.YingwuZhu Sorting Considerlist...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online