lecture10

# lecture10 - Sorting Dr.YingwuZhu Sorting Considerlist...

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

Sorting Dr. Yingwu Zhu

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

View Full Document
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
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

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

View Full Document
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)?
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) = ?

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

View Full Document
Simple Selection Sort Algorithm Linked List-based
Simple Selection Sort Algorithm template <typename T> void selection_sort(Node* head) {     while (head) {        Node* ptr = head->next;        while (ptr) {            if (head->data > ptr->data) {

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/01/2011 for the course CSSE 250 taught by Professor Dr.yingwuzhu during the Fall '11 term at UH Clear Lake.

### Page1 / 25

lecture10 - Sorting Dr.YingwuZhu Sorting Considerlist...

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

View Full Document
Ask a homework question - tutors are online