Download presentation

Presentation is loading. Please wait.

Published byBernadette Elliott Modified over 2 years ago

2
Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005? B Smith: Skipped Spring 2005?

3
B. Smith2 Overview Sorting Selection Sort –Animation –Code –Analysis of Selection Sort Exchange/Bubble Sort –Animation –Code –Analysis of Bubble Sort

4
B. Smith3 Sorting An array a is sorted (ascending order) if: for all i 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

5
B. Smith4 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?

6
B. Smith5 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!

7
B. Smith6 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. 1 5 8 7 9 2 4 2 1 4 4 9 5 7 7 8 8 9 find min 1 5 8 7 9 2 4 input

8
B. Smith7 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]); } min to be index of smallest value Find smallest of entire list Swap smallest with next unsorted I = 0 I = 1I =2 Selection Sort Code (RS) Implementation of Selection Sort: void swap(int* a, int* b) { int t; t= *a; *a = *b; *b = t; return;}

9
B. Smith8 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 In general, total number of iterations (& comparisons) is: (N - 1) + (N - 2) + (N - 3) + … + 2 + 1 = N(N - 1)/2 N = number of elements in array Selection Sort Analysis

10
B. Smith9 Selection Sort Analysis What is the worst input for selection sort? Analysis of the worst case: –Outer loop executes N-1 times (no exch rqd. for final element). –Inner loop executes N-i-1 comparisons. (N-1) + (N-2) + … + 2 + 1 = N*(N-1)/2 = O(N 2 ) Max number of exchanges is proportional to array size (one exchange per outer loop). Worst case: Advantages of selection sort: –Easy to write –Can be done “in place” –Can be done on linked lists too (keep a tail pointer) Disadvantage: –It is about N 2 even in the best case for comparisons –So the running time (over all inputs) is approximately O(N 2 )

11
B. Smith10 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?

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

13
B. Smith12 Bubble Sort – pass 1 6903073215542669030732155426 30769032690155690426690

14
B. Smith13 69030732155426690307321554263076903215542630732690155426307321556904263073215542669030732155426690 Bubble Sort pass 1

15
B. Smith14 Bubble Sort - pass 2 30732155426690307321554266903230715542669032155307426690

16
B. Smith15 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

17
B. Smith16 Bubble Sort – Analysis Similar to Selection Sort –number of comparisons is O(N 2 ). –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

18
B. Smith17 Bubble Sort Code (RS) 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]); } #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define compswap(A, B) if(less(B, A)) swap(A, B) B Smith: These macros may cause problems. Consider converting to subroutines B Smith: These macros may cause problems. Consider converting to subroutines

19
B. Smith18 Summary Sorting Selection Sort Animation Code Analysis of Selection Sort Exchange/Bubble Sort Animation Code Analysis Bubble Sort Analysis

20
B. Smith19 Extra Slides

21
B. Smith20 Generic Sort Template #include 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"); }

Similar presentations

Presentation is loading. Please wait....

OK

3 – SIMPLE SORTING ALGORITHMS

3 – SIMPLE SORTING ALGORITHMS

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on inhabiting other planets in the solar Ppt on e-banking project Ppt on ram and rom pictures Ppt on production system in ai Ppt on creativity and innovation management Ppt on different types of dance forms computer Ppt on as 14 amalgamation theory Ppt on pi in maths what is the product Ppt on global warming and climate changes Ppt on waves tides and ocean currents diagram