1 / 18

Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04

B Smith: Save until Week 15?. Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04. B Smith: Skipped Spring 2005?. Overview. Sorting Selection Sort Animation Code Analysis of Selection Sort Exchange/Bubble Sort Animation Code Analysis of Bubble Sort. Sorting.

yonah
Download Presentation

Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. B Smith: Save until Week 15? Math 130Introduction to ComputingSortingLecture # 1710/11/04 B Smith: Skipped Spring 2005?

  2. Overview • Sorting • Selection Sort • Animation • Code • Analysis of Selection Sort • Exchange/Bubble Sort • Animation • Code • Analysis of Bubble Sort B. Smith

  3. Sorting • An array a is sorted (ascending order) if: for all i < j => a[i]  a[j] • Probably the most well-studied algorithmic problem in Computer Science • There are many algorithms • We will study 2 (others will be studied in M140) • Why not just one? • Historical • Differ in ease of programming • Differ in performance • What’s “best” sometimes depends on the problem B. Smith

  4. Sorting in the Real World • Sorting is one of the most common operations in “data mining” • Often need to sort enormous sets of data • Don’t fit in memory (RAM) • These are called external sorts – need to store intermediate results on disks • Can use multiple machines (parallelism) to make sorting run faster • Divide and Conquer algorithms (M140) • When you are faced with the job of sorting: • What sort routines are available? • The “quicksort” algorithm is commonly available on many platforms • time is proportional to N*log( N ) on average to sort N items • What does your data look like? • partially sorted? • reverse order sorted 90% of the time? B. Smith

  5. What Affects Choice of Algorithm? • How large is the set of things? • Are you sorting an array or a linked list? • Are you sorting it all at once, or are elements coming in one at a time (incremental)? • Is the data likely to be (nearly) sorted when you start? • How much time to do you have to write and debug the code? • How much extra space can you afford, or (related) how expensive is object allocation/deallocation? • An “in-place” sorting algorithm uses only O(1) space in addition to the input data structure • Are there sorting implementations you can use? • Big idea -- don't reinvent the wheel! B. Smith

  6. 4 4 2 2 9 9 7 7 8 8 5 5 1 1 1 4 4 9 5 7 7 8 Selection Sort • Selection sort idea: • find smallest element in the array and exchange it with the element in the first position. • find second smallest element and exchange it with the element in the second position. • Do this (n-1) times. input 2 8 9 find min B. Smith

  7. Selection Sort Code (RS) Implementation of Selection Sort: min to be index of smallest value Find smallest of entire list Swap smallest with next unsorted • void selection(int a[], int l, int r) • { • for (int i = l; i < r; i++) • { • int min = i; • for (int j = i+1; j <= r; j++) • if (a[j] < a[min]) min = j; • swap(&a[i], &a[min]); • } • } void swap(int* a, int* b) { int t; t= *a; *a = *b; *b = t; return;} I =2 I = 1 I = 0 B. Smith

  8. Selection Sort Analysis N = number of elements in array • For I = 0 • for j=1, j <= N, find min • The inner j loop iterates (N-1) times • For I = 1 • for j=2, j <= N, find min • The inner j loop iterates (N-2) times • For I = 2 • for j=3, j <= N, find min • The inner j loop iterates (N-3) times e.g., N=5, Iterate thru a[1] to a[4], 4 loops e.g., Iterate thru a[2] to a[4], 3 loops e.g., Iterate thru a[3] to a[4], 2 loops For the last subarray, the number of iterations is just 1 In general, total number of iterations (& comparisons) is: (N - 1) + (N - 2) + (N - 3) + … + 2 + 1 = N(N - 1)/2 B. Smith

  9. Improving Selection Sort • Can find the min in a balanced tree in O(log n) • May use a tree to improve the insert time for insertion sort or find-min time for selection sort • Produce O(n log n) sorting algorithms • But if we’re sorting an array, that’s a lot of code • And the constant may be high for object allocation and pointer manipulation • Can find a min in O(log n) time using a “heap” • Heap sort like selection sort, but maintains unsorted part in heap • Complicated to write, but can be done in-place • Selection sort finds minimum by linear search • Can we do better than this? B. Smith

  10. Exchange (“Bubble”) Sort (RS) • Bubble Sort Idea • Elements next to each other are swapped so that the list gets sorted • Start with comparison of first two elements • For ascending order, smaller value is placed before larger value • For descending order,smaller value is placed after larger value • Continue until last two items are compared and evaluated for exchange • When you can go through the list with no exchanges, the elements are sorted B. Smith

  11. 690 307 32 155 426 690 307 32 155 426 307 307 307 307 307 32 32 32 690 32 155 155 690 155 32 426 426 155 690 155 426 690 426 690 426 Bubble Sort pass 1 B. Smith

  12. 307 307 32 32 32 155 32 307 155 155 307 155 426 426 426 426 690 690 690 690 Bubble Sort - pass 2 B. Smith

  13. Bubble Sort • Note that the largest value always sank to the bottom of list • The smaller elements slowly rose or “bubbled” up from the bottom • Each pass always places the largest in the list at the bottom • Hence, no need to do “compare and exchange” for the final two elements • For each pass, one less compare/exchange operation is required B. Smith

  14. Bubble Sort – Analysis • Similar to Selection Sort • number of comparisons is O(N2). • number of moves depends on initial order of the values in the list • Worst case analysis • Occurs when list items are in reverse sorted order • this is when Selection Sort beats Bubble Sort. • Although both require N(N-1)/2 comparisons… • Selection Sort needs only N-1 moves • Bubble Sort needs N(N-1)/2 moves • For random data • Selection Sort generally performs as well as or better than bubble sort B. Smith

  15. B Smith: These macros may cause problems. Consider converting to subroutines Bubble Sort Code (RS) #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define compswap(A, B) if(less(B, A)) swap(A, B) void bubble(int a[], int l, int r) { int i, j; for (i = l; i < r; i++) for (j = r; j > i; j--) compswap(a[j-1], a[j]); } B. Smith

  16. Summary • Sorting • Selection Sort • Animation • Code • Analysis of Selection Sort • Exchange/Bubble Sort • Animation • Code • Analysis • Bubble Sort Analysis B. Smith

  17. Extra Slides B. Smith

  18. #include <stdio.h> #include <stdlib.h> typedef int Item; #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define exch(A, B) { Item t = A; A = B; B = t; } #define compexch(A, B) if (less(B, A)) exch(A, B) void sort(Item a[], int l, int r) //Insertion Sort (R.S.) { int i, j; for (i = l+1; i <= r; i++) for (j = i; j > l; j--) compexch(a[j-1], a[j]); } // This program takes optional command line arguments as input // e.g. "Sort01 5 1" will give you 5 random integers and // "Sort01 99 1" will give you 99 random integers // and "Sort01" will allow you to input as many integers as you desire main(int argc, char *argv[]) { int i, N = atoi(argv[1]), sw = atoi(argv[2]); //srand(time(NULL)); int *a = malloc(N*sizeof(int)); /*dynamic array allocation */ if (sw) for (i = 0; i < N; i++) a[i] = 1000*(1.0*rand()/RAND_MAX); else for (N = 0; scanf("%d", &a[N]) == 1 ; N++) ; //enter floating pt number or a character to quit entry sort(a, 0, N-1); // Generic enough to use with any Sort function for (i = 0; i < N; i++) printf("%3d ", a[i]); printf("\n"); } Generic Sort Template B. Smith

More Related