Sorting & Searching. Hours: 10. Marks: 16

Similar documents
BITS PILANI, DUBAI CAMPUS DUBAI INTERNATIONAL ACADEMIC CITY, DUBAI FIRST SEMESTER

Arrays a kind of data structure that can store a fixedsize sequential collection of elements of the same type. An array is used to store a collection

Lecture 6 Sorting and Searching

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

UNIT III ARRAYS AND STRINGS

UNIT 7. SEARCH, SORT AND MERGE

Bubble sort starts with very first two elements, comparing them to check which one is greater.

Sorting and Searching Algorithms

CSCI 2132 Software Development Lecture 18: Implementation of Recursive Algorithms

LECTURE NOTES ON DATA STRUCTURES THROUGH C

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Faster Sorting Methods

Sorting. Task Description. Selection Sort. Should we worry about speed?

DATA STRUCTURES AND ALGORITHMS

Cpt S 122 Data Structures. Sorting

Sorting Algorithms. + Analysis of the Sorting Algorithms

O(n): printing a list of n items to the screen, looking at each item once.

Algorithm for siftdown(int currentposition) while true (infinite loop) do if the currentposition has NO children then return

CSC 273 Data Structures

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

Sorting. Bubble Sort. Pseudo Code for Bubble Sorting: Sorting is ordering a list of elements.

4. SEARCHING AND SORTING LINEAR SEARCH

PERFORMANCE OF VARIOUS SORTING AND SEARCHING ALGORITHMS Aarushi Madan Aarusi Tuteja Bharti

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

CS 137 Part 8. Merge Sort, Quick Sort, Binary Search. November 20th, 2017

Chapter 7 Sorting. Terminology. Selection Sort

Sorting. Sorting in Arrays. SelectionSort. SelectionSort. Binary search works great, but how do we create a sorted array in the first place?

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

CSE101-lec#19. Array searching and sorting techniques. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

Internal Sort by Comparison II

Sorting. Weiss chapter , 8.6

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

COMP1511 focuses on writing programs. Effciency is also important. Often need to consider:

Columns A[0] A[0][0] = 20 A[0][1] = 30

DATA STRUCTURES AND ALGORITHMS

International Journal Of Engineering Research & Management Technology

Data Structure & Algorithms Laboratory Manual (CS 392)

ECE 2574: Data Structures and Algorithms - Basic Sorting Algorithms. C. L. Wyatt

CS 310 Advanced Data Structures and Algorithms

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

Analysis of Algorithms. Unit 4 - Analysis of well known Algorithms

Sorting. Sorting. Stable Sorting. In-place Sort. Bubble Sort. Bubble Sort. Selection (Tournament) Heapsort (Smoothsort) Mergesort Quicksort Bogosort

Internal Sort by Comparison II

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

DEV BHOOMI INSTITUE OF TECHNOLOGY DEHRADUN Department Of Computer Application. Design and Analysis of Algorithms Lab MCA-302

Sorting. Two types of sort internal - all done in memory external - secondary storage may be used

Internal Sort by Comparison II

Searching an Array: Linear and Binary Search. 21 July 2009 Programming and Data Structure 1

SAMPLE OF THE STUDY MATERIAL PART OF CHAPTER 6. Sorting Algorithms

Divide and Conquer Sorting Algorithms and Noncomparison-based

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

Quicksort. Repeat the process recursively for the left- and rightsub-blocks.

Data Structures and Algorithms (DSA) Course 12 Trees & sort. Iulian Năstac

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Sorting Pearson Education, Inc. All rights reserved.

Sorting and Searching

IS 709/809: Computational Methods in IS Research. Algorithm Analysis (Sorting)

7 Sorting Algorithms. 7.1 O n 2 sorting algorithms. 7.2 Shell sort. Reading: MAW 7.1 and 7.2. Insertion sort: Worst-case time: O n 2.

2/14/13. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

Sorting algorithms Properties of sorting algorithm 1) Adaptive: speeds up to O(n) when data is nearly sorted 2) Stable: does not change the relative

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Search,Sort,Recursion

Divide and Conquer Algorithms: Advanced Sorting. Prichard Ch. 10.2: Advanced Sorting Algorithms

CS 2412 Data Structures. Chapter 10 Sorting and Searching

Design and Analysis of Algorithms Assignment-2 Solutions (Hints) REMARK: Quicksort

COMP Data Structures

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Sorting and Selection

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

! Search: find a given item in a list, return the. ! Sort: rearrange the items in a list into some. ! list could be: array, linked list, string, etc.

Sorting. Hsuan-Tien Lin. June 9, Dept. of CSIE, NTU. H.-T. Lin (NTU CSIE) Sorting 06/09, / 13

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Sorting. Bringing Order to the World

CSE 373: Data Structures and Algorithms

Divide and Conquer Algorithms: Advanced Sorting

CSE 373: Data Structures and Algorithms

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

9/10/12. Outline. Part 5. Computational Complexity (2) Examples. (revisit) Properties of Growth-rate functions(1/3)

COMP2012H Spring 2014 Dekai Wu. Sorting. (more on sorting algorithms: mergesort, quicksort, heapsort)

Sorting. Order in the court! sorting 1

Arrays and Applications

Introduction to Arrays

CSE 143 Lecture 16 (B)

Prof. Sushant S Sundikar 1

Sorting. Riley Porter. CSE373: Data Structures & Algorithms 1

Sorting. CSE 143 Java. Insert for a Sorted List. Insertion Sort. Insertion Sort As A Card Game Operation. CSE143 Au

Algorithms Lab (NCS 551)

CSCI 2132 Software Development. Lecture 19: Generating Permutations

Procedural Programming

Sorting is ordering a list of objects. Here are some sorting algorithms

CSE 143. Two important problems. Searching and Sorting. Review: Linear Search. Review: Binary Search. Example. How Efficient Is Linear Search?

Table ADT and Sorting. Algorithm topics continuing (or reviewing?) CS 24 curriculum

Sorting (I) Hwansoo Han

Parallel Sorting Algorithms

Data Structures and Algorithms

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Instructor: X. Zhang

a) State the need of data structure. Write the operations performed using data structures.

Lecture 19 Sorting Goodrich, Tamassia

Divide and Conquer CISC4080, Computer Algorithms CIS, Fordham Univ. Acknowledgement. Outline

Transcription:

Sorting & Searching CONTENTS 2.1 Sorting Techniques 1. Introduction 2. Selection sort 3. Insertion sort 4. Bubble sort 5. Merge sort 6. Radix sort ( Only algorithm ) 7. Shell sort ( Only algorithm ) 8. Quick sort ( Only algorithm ) 2.2 Searching 1. Linear search 2. Binary search Hours: 10 Marks: 16

2.1 Sorting Techniques 1. Introduction i. Sorting is a process that organizes a collection of data into either ascending or descending order. ii. An internal sort requires that the collection of data fit entirely in the computer s main memory. iii. We use an external sort when the collection of data cannot fit in the computer s main memory all at once but must reside in secondary storage such as on a disk. iv. We will analyze only internal sorting algorithms. v. Any significant amount of computer output is generally arranged in some sorted order so that it can be interpreted. Anuradha Bhatia 2

2. Selection Sort (Question: Explain the concept of selection sort with program, advantages and its disadvantages 8 Marks) i. The list is divided into two sub lists, sorted and unsorted, which are divided by an imaginary wall. ii. We find the smallest i.e the minimum element from the unsorted sub list and swap it with the element at the beginning of the unsorted data. iii. After each selection and swapping, the imaginary wall between the two sub lists move one element ahead, increasing the number of sorted elements and decreasing the number of unsorted ones. iv. Each time we move one element from the unsorted sub list to the sorted sub list, we say that we have completed a sort pass. v. A list of n elements requires n-1 passes to completely rearrange the data. Original List After Pass 1 After Pass 2 After Pass 3 After Pass 4 After Pass 5 Figure 1: Selection Sort Advantages i. The main advantage of the selection sort is that it performs well on a small list. ii. It is an in-place sorting algorithm, no additional temporary storage is required beyond what is needed to hold the original list. Anuradha Bhatia 3

Disadvantages i. The primary disadvantage of the selection sort is its poor efficiency when dealing with a huge list of items. ii. Its performance is easily influenced by the initial ordering of the items before the sorting process. WAP to sort the elements using selection sort #include <stdio.h> void main() int array[100], n, i, j, min, swap; printf("enter number of elements\n"); scanf("%d", &n); printf("enter %d integers\n", n); for ( i = 0 ; i < n ; i++ ) scanf("%d", &array[i]); i=o while(i<n) min = a[i]; for(j=i+1; j < 5; j++) if(min > a[j]) min = a[j]; temp = a[i]; a[i] = a[j]; a[j] temp; i++; printf("sorted list in ascending order:\n"); for ( i = 0 ; i < n ; i++ ) printf("%d\n", array[i]); Anuradha Bhatia 4

3. Insertion Sort (Question: Explain the concept of insertion sort with program, advantages and its disadvantages 8 Marks) i. It always maintains two zones in the array to be sorted: sorted and unsorted. ii. At the beginning the sorted zone consist of one element (the first element of array that we are sorting). iii. On each step the algorithms expand the sorted zone by one element inserting the first element from the unsorted zone in the proper place in the sorted zone and shifting all larger elements one slot down. Advantages i. The insertion sorts repeatedly scans the list of items, each time inserting the item in the unordered sequence into its correct position. ii. The main advantage of the insertion sort is its simplicity. It also exhibits a good performance when dealing with a small list. The insertion sort is an in-place sorting algorithm so the space requirement is minimal. Disadvantage i. The disadvantage of the insertion sort is that it does not perform as well as other, better sorting algorithms. ii. With n-squared steps required for every n element to be sorted, the insertion sort does not deal well with a huge list. iii. Therefore, the insertion sort is particularly useful only when sorting a list of few items. Figure 2: Insertion Sort Anuradha Bhatia 5

WAP to sort the elements using insertion sort. #include<stdio.h> #include<conio.h> void main() int i, j, temp,a[10]; printf( enter the elements ); for(i = 0; i <= 10; i ++) scanf( %d, &a[i]); for (i = 0; i <= 10; i++) for(j = i +1;j <= 10; j++) if(a[i] > a[j]) temp = a[i]; while( j!= i) a[j] = a[j-1]; j --; a[i] = temp; printf ( the sorted array is ); for( i = 0; i <=10; i++) printf( %d \n, a[i]); Anuradha Bhatia 6

4. Bubble Sort (Question: Explain the concept of bubble sort with program, advantages and its disadvantages 8 Marks) i. Bubble sort algorithms cycle through a list, analyzing pairs of elements from left to right, or beginning to end. ii. If the leftmost element in the pair is less than the rightmost element, the pair will remain in that order. If the rightmost element is less than the leftmost element, then the two elements will be switched. iii. This cycle repeats from beginning to end until a pass in which no switch occurs. Advantages i. The bubble sort requires very little memory other than that which the array or list itself occupies. ii. The bubble sort is comprised of relatively few lines of code. iii. With a best-case running time of O(n), the bubble sort is good for testing whether or not a list is sorted or not. Other sorting methods often cycle through their whole sorting sequence, which often have running times of O(n^2) or O(n log n) for this task. iv. The same applies for data sets that have only a few items that need to be swapped a few times. Disadvantages i. The main disadvantage of the bubble sort method is the time it requires. ii. With a running time of O (n^2), it is highly inefficient for large data sets. Anuradha Bhatia 7

WAP to sort the elements in the array using bubble sort. #include <stdio.h> int main() int array[100], n, i, j, temp; printf("enter number of elements\n"); scanf("%d", &n); printf("enter %d integers\n", n); for (i = 0; j < n; i++) scanf("%d", &array[i]); for (i = 0 ; i < ( n - 1 ); i++) for (j = i+1 ; j < n; j++) if (array[i] > array[j]) /* For decreasing order use < */ temp = array[i]; array[i] = array[j]; array[j] = temp; printf("sorted list in ascending order:\n"); for ( c = 0 ; c < n ; c++ ) printf("%d\n", array[c]); return 0; Anuradha Bhatia 8

5. Merge Sort (Question: Explain the concept of merge sort with program, advantages and its disadvantages 8 Marks) i. It follows the fundamental of dividing the elements and sorting, as sorting is easier in smaller bits of elements. ii. iii. iv. If the sub list is 1 in length, then the sub list is sorted. If the list is more than one in length, then divide the unsorted list into roughly two parts. Keep dividing the sub lists until each one is only 1 item in length. v. Now merge the sub-lists back into a list twice their size, at the same time sorting each items into order. vi. Keep merging the sub list until the list is complete once again. Advantage i. Good for sorting slow-access data. ii. It is excellent for sorting data that are normally accessed sequentially. Disadvantage i. If the list is N long, then it takes 2*N memory space to sort the elements. ii. For large data its time and space complexity is of the order of nlogn. WAP to sort the elements in the array using merge sort. #include<stdio.h> #define MAX 50 void mergesort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main() int merge[max],i,n; printf("enter the total number of elements: "); scanf("%d",&n); printf("enter the elements which to be sort: "); for(i=0;i<n;i++) scanf("%d",&merge[i]); partition(merge,0,n-1); Anuradha Bhatia 9

printf("after merge sorting elements are: "); for(i=0;i<n;i++) printf("%d ",merge[i]); return 0; void partition(int arr[],int low,int high) int mid; if(low<high) mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); mergesort(arr,low,mid,high); void mergesort(int arr[],int low,int mid,int high) int i,m,k,l,temp[max]; l=low; i=low; m=mid+1; while((l<=mid)&&(m<=high)) if(arr[l]<=arr[m]) temp[i]=arr[l]; l++; else temp[i]=arr[m]; m++; i++; Anuradha Bhatia 10

if(l>mid) for(k=m;k<=high;k++) temp[i]=arr[k]; i++; else for(k=l;k<=mid;k++) temp[i]=arr[k]; i++; for(k=low;k<=high;k++) arr[k]=temp[k]; Figure 3: Merge Sort Anuradha Bhatia 11

6. Radix Sort (Question: Explain the concept of radix sort the given series of numbers 8 Marks) i. The array for the bucket sort is divided in to 10 buckets from 0 to 9. ii. This sort is commonly also known as bucket sort. iii. The numbers are sorted on the LSB (Least Significant Bit) of any number for the first pass. iv. The graphical representation of the same is as shown below Bucket 0 1 2 3 4 5 6 7 8 9 Content 1 - - 64 81 4 25 36 16 - - 9 49 Figure 4: Radix Sort Pass 1 Pass 1: 1, 81, 64, 4, 25, 36, 16, 9, and 49. v. This sort is implemented for the good functionality for a three bit number. vi. After the Pass 1 print the list and continue with the same on the middle bit and put in the respective buckets. Bucket 0 1 2 3 4 5 6 7 8 9 Content 01 04 09 Figure 5: Radix Sort Pass 2 16 25 36 49 64 81 vii. Then print the pass 2. Pass2: 01, 04, 09, 16, 25, 36, 49, 64, 81 Anuradha Bhatia 12

7. Shell Sort (Question: Explain the concept of shell sort the given series of numbers 8 Marks) i. The shell sort, sometimes called the diminishing increment sort, improves on the insertion sort by breaking the original list into a number of smaller sub lists, each of which is sorted using an insertion sort. ii. The unique way that these sub lists are chosen is the key to the shell sort. iii. Instead of breaking the list into subsists of contiguous items, the shell sort uses an increment i, sometimes called the gap, to create a sub list by choosing all items that are i items apart. iv. This can be seen in Figure 6. This list has nine items. v. If we use an increment of three, there are three sub lists, each of which can be sorted by an insertion sort. vi. After completing these sorts, we get the list shown in Figure 7. vii. Although this list is not completely sorted, something very interesting has happened. By sorting the sub lists, we have moved the items closer to where they actually belong. Figure 6: Shell sort with increment of 3 Anuradha Bhatia 13

Figure 7: Shell Sort after sorting each sub list viii. Figure 8 shows a final insertion sort using an increment of one; in other words, a standard insertion sort. ix. Note that by performing the earlier sub list sorts, we have now reduced the total number of shifting operations necessary to put the list in its final order. x. For this case, we need only four more shifts to complete the process. Figure 8: sorted list after increment of 1 Anuradha Bhatia 14

8. Quick Sort (Question: Explain the concept of quick sort the given series of numbers 8 Marks) i. The quicksort algorithm partitions an array of data into items that are less than the pivot and those that are greater than or equal to the pivot ii. iii. item. The first step is to create the partitions, in which a random pivot item is created, and the partition algorithm is applied to the array of items. This initial step is the most difficult part of the Quicksort algorithm. Advantages i. The efficient average case compared to any aforementioned sort algorithm, as well as the elegant recursive definition, and the popularity due to its high efficiency. ii. The quick sort produces the most effective and widely used method of sorting a list of any item size. Disadvantages i. The difficulty of implementing the partitioning algorithm and the average efficiency for the worst case scenario, which is not offset by the difficult implementation. ii. Quicksort works by "partitioning" the array to be sorted, then recursively sorting each partition. Here is the pseudocode. procedure QuickSort (array A, int L, int N) if L < N M := Partition (A, L, N) QuickSort (A, L, M - 1) QuickSort (A, M + 1, N) endif endprocedure Int function Partition (array A, int L, int N) select M, where L <= M <= N reorder A(L)... A(N) so that I < M implies A(I) <= A(M), and I > M implies A(I) >= A(M) return M endfunction Anuradha Bhatia 15

Name Average Worst Space Stable Method Bubble sort O(n 2 ) O(n 2 ) O(1) Yes Exchanging Selection sort O(n 2 ) O(n 2 ) O(1) No Selection Insertion sort O(n 2 ) O(n 2 ) O(1) Yes Insertion Merge sort O(n log n) O(n log n) O(n) Yes Merging Quicksort O(n log n) O(n 2 ) O(1) No Partitioning Heap sort O(n log n) O(n log n) O(1) No Selection Table 1: Time and Space complexity Figure 9: Graphical representation of complexity Anuradha Bhatia 16

2.2 Searching 1. Linear Search (Question: Explain the concept of linear the given series of numbers 8 Marks) i. This is the simplest searching technique available. ii. To search whether an element is present or absent in the array, we compare it with each and every other element in the array. iii. If the element is found after comparison than it is printed as element found, else element not found or absent in the array. Advantage i. This is the simplest searching algorithm ii. The space complexity is very less. iii. Efficient for small data, if the dataset created is small. Disadvantage i. The time complexity increases. ii. The number of comparisons become large as n, for the large set of array. WAP to check whether an element is found in the array or not. #include<stdio.h> int main() int a[10],i,n,m,c=0; printf("enter the size of an array: "); scanf("%d",&n); printf("enter the elements of the array: "); for(i=0;i<=n-1;i++) scanf("%d",&a[i]); printf("enter the number to be search: "); scanf("%d",&m); for(i=0;i<=n-1;i++) if(a[i]==m) c=1; break; Anuradha Bhatia 17

if(c==0) printf("the number is not in the list"); else printf("the number is found"); return 0; WAP to find the element present in the array and also print its location. #include <stdio.h> int main() int array[100], search, c, n; printf("enter the number of elements in array\n"); scanf("%d",&n); printf("enter %d integer(s)\n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("enter the number to search\n"); scanf("%d", &search); for (c = 0; c < n; c++) if (array[c] == search) /* if required element found */ printf("%d is present at location %d.\n", search, c+1); break; if (c == n) printf("%d is not present in array.\n", search); return 0; Anuradha Bhatia 18

2. Binary Search (Question: Explain the concept of binary search for the given series of numbers 8 Marks) i. A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found. ii. It is the classic example of a "divide and conquer" algorithm. WAP to search the element in the array using binary search #include <stdio.h> void main () int c, first, last, middle, n, search, array[100]; printf("enter number of elements\n"); scanf("%d",&n); printf("enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("enter value to find\n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) if (array[middle] < search) first = middle + 1; else if (array[middle] == search) printf("%d found at location %d.\n", search, middle+1); break; else last = middle - 1; middle = (first + last)/2; if (first > last) printf("not found! %d is not present in the list.\n", search); Anuradha Bhatia 19