Presentation is loading. Please wait.

Presentation is loading. Please wait.

DATA AND FILE STRUCTURE USING C MCA110 M K Pachariya Id. Department of Computer Application, Galgotias.

Similar presentations


Presentation on theme: "DATA AND FILE STRUCTURE USING C MCA110 M K Pachariya Id. Department of Computer Application, Galgotias."— Presentation transcript:

1 DATA AND FILE STRUCTURE USING C MCA110 M K Pachariya Email Id. manoj.pachariya@galgotiasuniversity.edu.in Department of Computer Application, Galgotias University, Greater Noida www.sites.google.com/a/galgotiasuniversity.edu.in/manojkumarpachariya L T P C 3 0 2 4

2 Topics to covered Internal Sort Bubble sort, Insertion sort, Selection sort, Quicksort, Mergesort. Shellsort,

3 Sorting Arranging the data in logical order. (Ascending/Descending for numeric and dictionary case for alphnumeric) Internal- deals with sorting data held in memory of computer. External- deals with data held in files. We cover Bubble, selection, insertion, Merge sort, quick sort, shell sort

4 Sorting Given a set (container) of n elements –E.g. array, set of words, etc. Suppose there is an order relation that can be set across the elements Goal Arrange the elements in ascending order –Start  1 23 2 56 9 8 10 100 –End  1 2 8 9 10 23 56 100

5 Bubble Sort Simplest sorting algorithm Idea: –1. Set flag = false –2. Traverse the array and compare pairs of two elements 1.1 If E1  E2 - OK 1.2 If E1 > E2 then Switch(E1, E2) and set flag = true –3. If flag = true goto 1. What happens?

6 Bubble sort It takes n-1 pass to sort the data. In each pass each element a[i] is compared with a[i+1] If a[i]> a[i+1] swap for ascending. This will causes last element move or bubble up. Analysis Total number of comparisions=f(n) F(n)=(n-1) +(n-2) + (n-3) + 3+2+1=n 2 O(n 2 )

7 Bubble sort Void bubblesort(int *a, int n) { int I, j, k, temp; For (i=1; i<n; i++) { for ( j=0; j<n-I;j++) { if (a[j] < a[j+1]) {Temp=a[j] A[j]=A[j+1] A[j+1]=temp}}}}

8 Bubble Sort 11 23 2 56 9 8 10 100 21 2 23 56 9 8 10 100 31 2 23 9 56 8 10 100 41 2 23 9 8 56 10 100 51 2 23 9 8 10 56 100 ---- finish the first traversal ---- ---- start again ---- 11 2 23 9 8 10 56 100 21 2 9 23 8 10 56 100 31 2 9 8 23 10 56 100 41 2 9 8 10 23 56 100 ---- finish the second traversal ---- ---- start again ---- …………………. Why Bubble Sort ?

9 Implement Bubble Sort with an Array void bubbleSort (Array S, length n) { boolean isSorted = false; while(!isSorted) { isSorted = true; for(i = 0; i<n; i++) { if(S[i] > S[i+1]) { int aux = S[i]; S[i] = S[i+1]; S[i+1] = aux; isSorted = false; }

10 Running Time for Bubble Sort One traversal = move the maximum element at the end Traversal #i : n – i + 1 operations Running time: (n – 1) + (n – 2) + … + 1 = (n – 1) n / 2 = O(n 2 ) When does the worst case occur ? Best case ?

11 Sorting Algorithms Using Priority Queues Remember Priority Queues = queue where the dequeue operation always removes the element with the smallest key  removeMin Selection Sort –insert elements in a priority queue implemented with an unsorted sequence –remove them one by one to create the sorted sequence Insertion Sort –insert elements in a priority queue implemented with a sorted sequence –remove them one by one to create the sorted sequence

12 Selection sort It requires n-1 pass Pass-1 Find the smallest element in elements a[0] …a[n-1] and swap with first element a[0] Pass-2 Find the smallest element in elements a[1] …a[n-1] and swap with second element a[1].. Pass-n-1 Find the loc of smaller elements a[n-2] & a[n-1] and swap the elements

13 Selection sort Void selectionsort(int a[], int n) { int temp, loc,I; For (i=1; i<=n-1;i++) {Selectsmallestelemen t(a,n,I,&loc); Temp=a[loc] A[loc]=a[i-1]; A[i-1]=temp}} Void selectsmallestelement(int a[]. Int n, int k, int *loc) {Int small,j Small=a[k-1] *Loc=k-1 For (j=k;j<n;j++) { if a[j]< small { small=a[j] *loc=j} }}

14 Selection Sort Largest key has the highest priority

15 Selection Sort void SelectionSort(List *list) { Position current; /* position of place being correctly filled */ Position max; /* position of largest remaining key */ for (current = list->count - 1; current > 0; current--) { max = MaxKey(0, current, list); Swap(max, current, list); } /* MaxKey: find the position of the largest key in the sublist. Swap: swap two entries in the list. */

16 Analysis In main loop, selection sort performs (n-1)+(n-2)+…+1 = n(n-1)/2 comparison keys. ~ n 2 /2 + O(n) O(n 2 )

17 Insertion sort It is very popular for bridge player when they first sort their cards. Pick up the particular element and place it at appropriate place in sorted list. Pass-1 A[1] is inserted before or after a[0] Pass-2 A[2] is inserted either a[0] or between a[0] and a[1] or after a[1] A[3] is inserted either before a[0] or between a[0] & a[1] or between a[1] & a[2] or after a[2] Continue till pass-n-1

18 Insertion sort Void insertionsort(int a[], int n) { int I, temp, j; For (i=1; i<=n-1;i++) { Temp=a[i] J=i-1 While(( temp =0)) {A[j+1]=a[j] J--;} A[j+1]=temp}}

19 Insertion Sort

20 insertion: O(1 + 2 + … + n) = O(n 2 ) selection: O(1 + 1 + … + 1) = O(n)

21 Basic Insertion Sort Algorithm 1.void insertion_sort(int input[], int array_size) 2.{ 3. int i,j, temp; 4. for (i=1; i<array_size; i++) 5. { 6. temp = input[i]; 7. for (j=I; temp < input[j-1]; j--) 8. input[j]=input[j-1]; 9. input[j] = temp; 10. } 11.} If data is presorted, this loop is never performed. Thus the running time is O(n) Two nested loop, each takes n iterations. Insertion sort is O(n 2 )

22 Linked Version Implementation SortediUnsorted < data[i]data [i]> data[i]Unsorted Needs 4 pointers: 1.Pointer to keep the last sorted node, *ls. 2.Pointer to keep the data node, that is the first unsorted node in the list (*fu). 3.Two pointers for searching and placing the unsorted node, *current and *trailing.

23 Linked Version Algorithm void InsertionSort(List *list) 1.{ 2. ListNode *fu; /* the first unsorted node to be inserted */ 3. ListNode *ls; /* the last sorted node (tail of sorted sublist) */ 4. ListNode *current, *trailing; 5. if (list->head) { 6. ls = list->head; /* An empty list is already sorted. */ 7. while (ls->next) { 8. fu = ls->next; /* Remember first unsorted node. */ 9. if (LT(fu->entry.key, list->head->entry.key)) { 10. ls->next = fu->next; 11. fu->next = list->head; 12. list->head = fu; /* Insert first unsorted at the head of sorted list. */ 13. } else { /* Search the sorted sublist. */

24 1.trailing = list->head; 2. for (current = trailing->next; GT(fu->entry.key, current->entry.key); 3. current = current->next) 4. trailing = current; 5. /* First unsorted node now belongs between trailing and current. */ 6. if (fu == current) 7. ls = fu; 8. else { 9. ls->next = fu->next; 10. fu->next = current; 11. trailing->next = fu; 12. } 13. } 14. } 15. } 16.}

25 Divide & conquer Next Sorting algorithms that rely on the “DIVIDE AND CONQUER” paradigm –One of the most widely used paradigms –Divide a problem into smaller sub problems, solve the sub problems, and combine the solutions –Learned from real life ways of solving problems

26 Divide-and-Conquer Divide and Conquer is a method of algorithm design that has created such efficient algorithms as Merge Sort. In terms or algorithms, this method has three distinct steps: –Divide: If the input size is too large to deal with in a straightforward manner, divide the data into two or more disjoint subsets. –Recur: Use divide and conquer to solve the subproblems associated with the data subsets. –Conquer: Take the solutions to the subproblems and “merge” these solutions into a solution for the original problem.

27 Merge sort Merge sort uses divide and conquer approach It divide the array into two halves and sort them separately then merge them. This procedure is recursive, with basic criteria- number of elements in array is not more than 1 If beg <end then Divide the list into two halves Mergesort the left half Mergesort the right half Merge the both sorted halves into one sorted list Endif O(nlog 2 n)

28 Merge Sort Void Mergesort (int a[], int beg, int end) {int mid; If beg <end then Mid=(beg+end)/2 Mergesort(a, beg, mid) Mergesort(a, mid+1, end) Mergingsortedarray(a,beg, mid, mid+1, end) endif Mergesort() Na=lb, nb=rb, nc=lb and nc=lb While((na<lr) &&(nb<rr)) {if (a[na] < a[nb]) {C[nc]=a[na] Na++} Else {{C[nc]=a[nb] Nb++} Nc++ } // end while {if (na > lr) While (nb<=rr) {C[nc]=a[nb] Nb++; nc++} Else {While (na<=lr) {C[nc]=a[na] Na++; nc++} For (i=lb;i<rr;i++) A[i]=c[i]

29 Merge-Sort Algorithm: –Divide: If S has at leas two elements (nothing needs to be done if S has zero or one elements), remove all the elements from S and put them into two sequences, S 1 and S 2, each containing about half of the elements of S. (i.e. S 1 contains the first  n/2  elements and S 2 contains the remaining  n/2  elements. –Recur: Recursive sort sequences S 1 and S 2. –Conquer: Put back the elements into S by merging the sorted sequences S 1 and S 2 into a unique sorted sequence. Merge Sort Tree: –Take a binary tree T –Each node of T represents a recursive call of the merge sort algorithm. –We associate with each node v of T a the set of input passed to the invocation v represents. –The external nodes are associated with individual elements of S, upon which no recursion is called.

30 Merge-Sort

31 Merge-Sort(cont.)

32 Merge-Sort (cont’d)

33 Merging Two Sequences

34 Merge Sort 26333529191222 26333529 353326 221219 26333529 26332935 26293335

35 Merge Sort 12192226293335 221912 26293335

36 Merge Sort 221912 29263533 aPtr bPtr cPtr 12 cPtr 221912 29263533 aPtr bPtr 1912 cPtr 221912 29263533 aPtr bPtr 221912 cPtr 221912 29263533 aPtr bPtr Requires extra space for merging, thus it is barely used for internal sorting.

37 Analysis Time to merge n numbers = time to do two recursive mergesorts of size n/2 + time to merge, which is linear. T(1) = 1 T(n) = 2T(n/2) + n O(n log n)

38 Quick-Sort Another divide-and-conquer sorting algorihm To understand quick-sort, let’s look at a high-level description of the algorithm 1) Divide : If the sequence S has 2 or more elements, select an element x from S to be your pivot. Any arbitrary element, like the last, will do. Remove all the elements of S and divide them into 3 sequences: L, holds S’s elements less than x E, holds S’s elements equal to x G, holds S’s elements greater than x 2) Recurse: Recursively sort L and G 3) Conquer: Finally, to put elements back into S in order, first inserts the elements of L, then those of E, and those of G. Here are some diagrams....

39 Quick sort Merge sort uses divide and conquer approach It divide the array into two halves and sort them separately then merge them. This procedure is recursive, with basic criteria- number of elements in array is not more than 1 If beg <end then Divide the list into two halves Quicksort the left half Quicksort the right half Endif

40 Quick Sort 1.If the number of data is 0 or 1, then return. pivot 2.Pick any element v in the list. This is called pivot. 3.Partition the list into two disjoint groups: –Lower list : contains all element < v –Higher list : contains all element > v 4.Recursively perform quick sort.

41 Idea of Quick Sort 1) Select: pick an element 2) Divide: rearrange elements so that x goes to its final position E 3) Recurse and Conquer: recursively sort

42 Quick Sort Void quicksort (int a[], int beg, int end) {int mid; If beg <end then Partitionarray(a,beg, end, &mid) Mergesort(a, beg, mid-1) Mergesort(a, mid+1, end) endif Mergesort() Na=lb, nb=rb, nc=lb and nc=lb While((na<lr) &&(nb<rr)) {if (a[na] < a[nb]) {C[nc]=a[na] Na++} Else {{C[nc]=a[nb] Nb++} Nc++ } // end while {if (na > lr) While (nb<=rr) {C[nc]=a[nb] Nb++; nc++} Else {While (na<=lr) {C[nc]=a[na] Na++; nc++} For (i=lb;i<rr;i++) A[i]=c[i]

43 Quick-Sort Tree

44 In-Place Quick-Sort Divide step: l scans the sequence from the left, and r from the right. A swap is performed when l is at an element larger than the pivot and r is at one smaller than the pivot.

45 In Place Quick Sort (cont’d) A final swap with the pivot completes the divide step

46 Quick Sort 26 33 35 29191222 Pivot = 26, then the list is partitioned into 19 12 22 and 33 35 29 pivot = 19: pivot = 33 12 and 22 29 and 35 Combing step 12 19 22 29 33 35 12 19 22 26 29 33 35

47 Choosing Pivot The best choice of pivot is the median value in the data list, but this is hard to find. The good estimation can be obtained by randomly choose 3 elements, and using the median of these three as pivot. O(n log n)

48 48 The steps of QuickSort 13 81 92 43 65 3157 26 75 0 S select pivot value 13 81 92 43 65 31 5726 75 0 S1S1 S2S2 partition S 13433157260 S1S1 819275 65 S2S2 QuickSort(S 1 ) and QuickSort(S 2 ) 1343315726065819275 S Presto! S is sorted [Weiss]

49 49 8149035276 0123456789 0149735268 ij QuickSort Example Choose the pivot as the median of three. Place the pivot and the largest at the right and the smallest at the left

50 50 QuickSort Example 0149735268 0149735268 ij 0149735268 ij 0142735968 ij ij Move i to the right to be larger than pivot. Move j to the left to be smaller than pivot. Swap

51 51 0142537968 ij 0142537968 ij 0142536978 ij S 1 < pivot pivot S 2 > pivot 0142735968 ij 0142735968 ij 0142537968 ij QuickSort Example

52 52 Recursive Quicksort Quicksort(A[]: integer array, left,right : integer): { pivotindex : integer; if left + CUTOFF  right then pivot := median3(A,left,right); pivotindex := Partition(A,left,right-1,pivot); Quicksort(A, left, pivotindex – 1); Quicksort(A, pivotindex + 1, right); else Insertionsort(A,left,right); } Don’t use quicksort for small arrays. CUTOFF = 10 is reasonable.

53 Running time analysis Average case analysis Worst case analysis What is the worst case for quick-sort? Running time?

54 Shell Sort

55 1.void ShellSort(List *list) 2.{ 3. int increment; /* spacing of entries in sublist */ 4. Position start; /* starting position of sublist */ 5. increment = list->count; 6. do { 7. increment = increment / 3 + 1; 8. for (start = 0; start < increment; start++) 9. SortInterval(start, increment, list); /* modified insert sort */ 10. } while (increment > 1); 11.}

56 56 Sorting: The Big Picture Given n comparable elements in an array, sort them in an increasing (or decreasing) order. Simple algorithms: O(n 2 ) Fancier algorithms: O(n log n) Comparison lower bound:  (n log n) Specialized algorithms: O(n) Handling huge data sets Insertion sort Selection sort Bubble sort Shell sort … Heap sort Merge sort Quick sort … Bucket sort Radix sort External sorting

57 10-57 Comparison of Sorting Algorithms

58 Summary Two classes of sorting algorithms –O(n 2 ) : insertion, selection and shell sort. –O(n log n) : merge and quick sort. Comparison of Methods –Use of space –Computational time –Ease of programming


Download ppt "DATA AND FILE STRUCTURE USING C MCA110 M K Pachariya Id. Department of Computer Application, Galgotias."

Similar presentations


Ads by Google