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

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
1 Merge Sort Review of Sorting Merge Sort. 2 Sorting Algorithms Selection Sort uses a priority queue P implemented with an unsorted sequence: –Phase 1:
Stephen P. Carl - CS 2421 Recursive Sorting Algorithms Reading: Chapter 5.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
DIVIDE AND CONQUER APPROACH. General Method Works on the approach of dividing a given problem into smaller sub problems (ideally of same size).  Divide.
Insertion Sort. Selection Sort. Bubble Sort. Heap Sort. Merge-sort. Quick-sort. 2 CPSC 3200 University of Tennessee at Chattanooga – Summer 2013 © 2010.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CSE 373: Data Structures and Algorithms
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Algorithms and Average Case Time Complexity
Sorting Chapter 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.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Insertion Sort Merge Sort QuickSort. Sorting Problem Definition an algorithm that puts elements of a list in a certain order Numerical order and Lexicographical.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
1 TCSS 342, Winter 2005 Lecture Notes Sorting Weiss Ch. 8, pp
Quicksort. 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N 2 ) n But, the worst case seldom.
CHAPTER 11 Sorting.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Internal.
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Divide and Conquer Sorting
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
CSE 373 Data Structures Lecture 19
Sorting (Part II: Divide and Conquer) CSE 373 Data Structures Lecture 14.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
CSCE 3110 Data Structures & Algorithm Analysis Sorting (I) Reading: Chap.7, Weiss.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
CSE 373: Data Structures and Algorithms Lecture 6: Sorting 1.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
1 Sorting Algorithms Sections 7.1 to Comparison-Based Sorting Input – 2,3,1,15,11,23,1 Output – 1,1,2,3,11,15,23 Class ‘Animals’ – Sort Objects.
Sorting Chapter 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.
Sorting Sorting: –Task of rearranging data in an order. –Order can be: Ascending Order: –1,2,3,4,5,6,7,8,9 Descending Order: –9,8,7,6,5,4,3,2,1 Lexicographic.
Sorting CSIT 402 Data Structures II. 2 Sorting (Ascending Order) Input ›an array A of data records ›a key value in each data record ›a comparison function.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
CENG 213 Data Structures Sorting Algorithms. CENG 213 Data Structures Sorting Sorting is a process that organizes a collection of data into either ascending.
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
Sorting divide and conquer. Divide-and-conquer  a recursive design technique  solve small problem directly  divide large problem into two subproblems,
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Sorting Algorithms Merge Sort Quick Sort Hairong Zhao New Jersey Institute of Technology.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Chapter 9: Sorting1 Sorting & Searching Ch. # 9. Chapter 9: Sorting2 Chapter Outline  What is sorting and complexity of sorting  Different types of.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Prof. U V THETE Dept. of Computer Science YMA
Description Given a linear collection of items x1, x2, x3,….,xn
SORTING Sorting is the process of arranging the elements in some logical order. Sorting are classified into following categories: External sorting: deals.
Chapter 4: Divide and Conquer
Advanced Sorting Methods: Shellsort
CSC215 Lecture Algorithms.
CSE 326: Data Structures Sorting
CSE 332: Data Abstractions Sorting I
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSE 332: Sorting II Spring 2016.
CSCE 3110 Data Structures & Algorithm Analysis
Sorting Popular algorithms:
Presentation transcript:

DATA AND FILE STRUCTURE USING C MCA110 M K Pachariya Id. Department of Computer Application, Galgotias University, Greater Noida L T P C

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

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

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  –End 

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?

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) =n 2 O(n 2 )

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}}}}

Bubble Sort finish the first traversal start again finish the second traversal start again ---- …………………. Why Bubble Sort ?

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; }

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 ?

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

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

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} }}

Selection Sort Largest key has the highest priority

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. */

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 )

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

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}}

Insertion Sort

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

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 )

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.

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. */

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.}

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

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.

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)

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]

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.

Merge-Sort

Merge-Sort(cont.)

Merge-Sort (cont’d)

Merging Two Sequences

Merge Sort

Merge Sort

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

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)

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....

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

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.

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

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]

Quick-Sort Tree

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.

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

Quick Sort Pivot = 26, then the list is partitioned into and pivot = 19: pivot = and and 35 Combing step

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 The steps of QuickSort S select pivot value S1S1 S2S2 partition S S1S S2S2 QuickSort(S 1 ) and QuickSort(S 2 ) S Presto! S is sorted [Weiss]

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 QuickSort Example ij ij ij ij Move i to the right to be larger than pivot. Move j to the left to be smaller than pivot. Swap

ij ij ij S 1 < pivot pivot S 2 > pivot ij ij ij QuickSort Example

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.

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

Shell Sort

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 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

10-57 Comparison of Sorting Algorithms

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