Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Data Structures and Algorithm: Searching and Sorting, Lecture notes of Data Structures and Algorithms

Contains A-Z material for searching and sorting which help prepare you for any question related to it

Typology: Lecture notes

2018/2019

Available from 04/28/2023

unnati-maheshwari
unnati-maheshwari 🇮🇳

3 documents

1 / 100

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
7. SORTING & SEARCHING
Syllabus:( Computer Engineering)
Sorting:
Sort Concept, Shell Sort, Radix sort,
Insertion Sort, Quick Sort, Merge Sort,
Heap Sort,
Searching:
List Search, Linear Index Search, Index
Sequential Search
Hashed List Search, Hashing Methods,
Collision Resolution
Sorting Sets, and Selection: ( Information
Technology )
Insertion Sort, Shell sort, Heap sort,
Quicksort, Bucket Sort, Merge Sort and
radix Sort, and A Lower Bound on
comparison-based Sorting and radix Sort,
the complexity of some sorting algorithms,
comparison of Sorting Algorithms.
A. SORTING :
Why sorting?
Before we learn sorting techniques, we must know why these
sorting techniques are used.
Sorting is the process of placing elements from a collection in
some kind of order. For example, a list of words could be sorted
alphabetically or by length. A list of cities could be sorted by
population, by area, or by zip code.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Data Structures and Algorithm: Searching and Sorting and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

7. SORTING & SEARCHING

Syllabus:( Computer Engineering) Sorting: Sort Concept, Shell Sort, Radix sort, Insertion Sort, Quick Sort, Merge Sort, Heap Sort, Searching: List Search, Linear Index Search, Index Sequential Search Hashed List Search, Hashing Methods, Collision Resolution

Sorting Sets, and Selection: ( Information Technology )

Insertion Sort, Shell sort, Heap sort, Quicksort, Bucket Sort, Merge Sort and radix Sort, and A Lower Bound on comparison-based Sorting and radix Sort, the complexity of some sorting algorithms, comparison of Sorting Algorithms.

A. SORTING :

Why sorting?

Before we learn sorting techniques, we must know why these sorting techniques are used.

Sorting is the process of placing elements from a collection in some kind of order. For example, a list of words could be sorted alphabetically or by length. A list of cities could be sorted by population, by area, or by zip code.

There are many, many sorting algorithms that have been developed and analyzed. This suggests that sorting is an important area of study in computer science. Sorting a large number of items can take a substantial amount of computing resources. Like searching, the efficiency of a sorting algorithm is related to the number of items being processed. For small collections, a complex sorting method may be more trouble than it is worth. The overhead may be too high. On the other hand, for larger collections, we want to take advantage of as many improvements as possible. In this section we will discuss several sorting techniques and compare them with respect to their running time.

Before getting into specific algorithms, we should think about the operations that can be used to analyze a sorting process. First, it will be necessary to compare two values to see which is smaller (or larger). In order to sort a collection, it will be necessary to have some systematic way to compare values to see if they are out of order. The total number of comparisons will be the most common way to measure a sort procedure. Second, when values are not in the correct position with respect to one another, it may be necessary to exchange them. This exchange is a costly operation and the total number of exchanges will also be important for evaluating the overall efficiency of the algorithm.

In this chapter we will learn some basic sorting techniques such as bubble sort , insertion sort , quick sort etc. to understand this we consider some basic terminologies : we will consider our array ( say array a[ ] ) of n elements. All the elements are of type

The basic idea of bubble sort is to traverse the file sequentially. We call them “ pass”. At each pass the element is compared with next successive element ( i.e. a[i] is always compared with a[ i+1] ). We swap them if they are in not proper order.

Initial pass of bubble sort starts from i = 0.

In above example the pass 1 can be described as:

Comparison Elements Result

a [0] and a[1] (25, 57) No swap

a [1] and a[2] (57,48) Swap

a [2] and a[3] (57,37) Swap

a [3] and a[4] (57,12) Swap

a [4] and a[5] (57,92) No swap

a [5] and a[6] (92,86) Swap

a [6] and a[7] (92,33) Swap

The position of elements after pass 1 will be :

25 48 37 12 57 86 33 92

Wait! The story doesn’t end here. We have more passes require to sort an elements. In next pass the value of i set to 1 (i = 1)

And the story repeats. Each and every element is again compared with successive element in an array.

You may ask one question that: how many passes you require to sort the elements?

Recall that our array size is n. i.e. we have total n elements to be sorted (0 to n-1 ). Therefore it is obvious that total n-1 iterations are required to sort complete set of elements.

Let us move our attention to previous example. Do not forget that we have completed only one pass. Rest passes are remaining. We will not go in detail with rest passes as it will increase size of your book by ultimately increasing the cost of your book! So I am saving your cost tabulating other passes.

Pass number Order of elements

1 [ 25 , 48, 37, 12, 57, 86, 33, 92 ]

2 [ 25 , 37, 12, 48, 57, 33, 86, 92 ]

3 [ 25 , 12, 37, 48, 33, 57, 86, 92 ]

4 [ 12 , 25, 37, 33, 48, 57, 86, 92 ]

5 [ 12 , 25, 33, 37, 48, 57, 86, 92 ]

6 [ 12 , 25, 33, 37, 48, 57, 86, 92 ]

7 [ 12 , 25, 33, 37, 48, 57, 86, 92 ]

//scan array from 0 to n-1 locations

for (i = 0 ; i <= n-1 ; i++)

{ printf("Enter element %d " , (i+1));

scanf("%d" , &a[i]);

} //sort the array

bubblesort(a,n); //print the sorted array

for (i=0 ; i <= n-1 ; i++)

printf("%d " , a[i]);

} //end of main

 Example :

This sorting technique requires n-1 passes to sort an array of n integers. It uses 2 counters i and j. First counter i varies from n- down to zero. For each value of i , the other counter j varies from 0 to i. For each value of j the following comparison is made

if (a[ j ] > a[ j+1])

if the above condition is true then swap a[j] and a[j+1].

Consider the array of 5 elements and the sorting process

25 2 34 4 13 0 1 2 3 4

Pass 1 of Bubble Sort

Counter i = n-2 that is i= Counter j will vary from 0 to i that is 0 to 3 For each value of j we check is a[j] > a[j+1] and if this condition is true we swap a[j] and a[j+1]

i=3 , j=0 , check a[0]>a[1] is true so swap a[0] and a[1]

2 25 34 4 13 0 1 2 3 4

i=3 , j=1 , check a[1]>a[2] is false do not swap a[1] and a[2]

2 25 34 4 13 0 1 2 3 4

i=3 , j=2 , check a[2]>a[3] is true so swap a[2] and a[3]

2 25 4 34 13 0 1 2 3 4

i=3 , j=3 , check a[3]>a[4] is true so swap a[3] and a[4]

2 25 4 13 34 0 1 2 3 4

Bubble sort is a blind sort i.e. it does not take care whether the elements are sorted or not. in all the type of complexity (Best/Worst/Average ) it gives O(n^2 ) as quadratic time.

To understand this, let us take an example of 5 elements as shown:

10 5 3 2 1

0 1 2 3 4

According to the bubble sort algorithm (refer above program) counters i and j will be vary and corresponding status of comparison will be made: (the situation is tabulated below)

Status of counter i Status of counter j Comparison

3 0 Yes

1 Yes

2 Yes

3 Yes

2 0 Yes

1 Yes

2 Yes

1 0 Yes

1 Yes

0 0 Yes

From above tabular data , we can conclude that when there are 5 elements present in an array total 10 ( 4+3+2+1) comparisons will be made. in general array of n size requires ( n-1)+ (n-2) + ( n-3) +.... + 3 + 2+ 1 comparisons.

In Best/ worst/average case Hence both the loops gets executed ‘n’ times where n= number of elements.

Therefore total number of comparisons at each pass: (n-1) + (n-

  1. +...... + 3+2+1 = n (n-1) /2 ~ n^2

Hence Time Complexity of Bubble Sort is O (n^2 ).

2. Selection Sort :

Selection sort is type of Divide & Conquer Strategy. We can consider first element as a smallest element (min element) in the array. Small element is then compared with all other element in array. If assumed small element > other element, then we swap the element with that value.

The algorithm divides the input list into two parts: the sub list of items already sorted, which is built up from left to right at the front (left) of the list, and the sub list of items remaining to be sorted that occupy the rest of the list. Initially, the sorted sub list is empty and the unsorted sub list is the entire input list. The algorithm proceeds by finding the smallest element in the

25 with 37 Min = 25

25 with 12 Min = 12

12 with 92 Min = 12

12 with 86 Min = 12

12 with 33 Min = 12

Therefore we swap the minimum element ( which is updated from pass 0 i.e. 12 ) with the original minimum element we have assumed i.e. 25.

The output of pass 0 is :

12 57 48 37 25 92 86 33

In the next pass (i.e. pass 1) the next minimum element considered is 57 i.e. a[1]. We compare with rest element in any array and story continues. Like bubble sort I will not waste your time to show each and every pass. All the passes are tabulated below:

Pass number Elements

0 [ 12 , 57 ,48, 37, 25 , 92, 86, 33 ]

1 [ 12 , 25 ,48, 37, 57 , 92, 86, 33 ]

2 [ 12 , 25 , 33 , 37, 57, 92, 86, 48 ]

3 [ 12 , 25 ,33, 37 , 57, 92, 86, 48 ]

4 [ 12 , 25 ,33, 37, 48, 92, 86, 57 ]

5 [ 12 , 25 ,33, 37, 48, 57 , 86, 92 ]

6 [ 12 , 25 ,33, 37, 48, 57, 86 , 92 ]

7 [ 12 , 25 ,33, 37, 48, 57, 86, 92 ]

 Implementation :

//program for selection sort void selectionsort(int a[] , int n) { int min, p , t , i , j;

for (i = 0 ; i <= n - 2; i++) { min = a[i] ; p = i; for (j = i + 1 ; j <= n - 1 ; j++) if (a[j] < min) { min = a[j]; p = j; }

//swap a[i] and a[p]

t = a[i]; a[i] = a[p];

In pass number i , we select minimum element from a[i] to a[n- 1] and then swap this minimum element with a[i].

Following steps show method of selection sort on array of 5 integers.

0 55 1 19 2 28 3 4 4 15

In pass 0 we select the minimum element from a[0] to a[n-1] which is 4 and swap this value with a[0]. So array after pass 0 is

0 4 1 19 2 28 3 55 4 15

In pass 1 we select the minimum element from a[1] to a[n-1] which is 15 and swap this value with a[1]. So array after pass 1 is

0 4 1 15 2 28 3 55 4 19

Similarly array after pass 2 is

Array after pass 3 is

0 4 1 15 2 19 3 28 4 55

This is a sorted array

 Analysis :

Best/ Worst /Average case Analysis :

Like Bubble sort even selection sort is a blind sort i.e. it does not take care whether the elements are sorted or not. In all the type of complexity (Best/Worst/Average) it gives O(n^2 ) as quadratic time.

To understand this, let us take an example of 5 elements as shown:

15 2 9 4 9

0 1 2 3 4

Therefore total number of comparisons at each pass: (n-1) + (n-

  1. +...... + 3+2+1 = n(n-1) /2 ~ n^2

Therefore Time complexity of Selection sort will be 0(n^2 )

3. Insertion Sort :

An insertion sort is one that sorts a set of records by inserting records into an existing sorted file.

In simple insertion sort, initially a [0] will be considered as sorted file of 1 element. In later stages (starting from a[1]) other elements are compared with other elements i.e. smallest element is get “inserted” into proper places of sorted file.

Sorted Unsorted

 Implementation :

//program for insertion sort void insertionsort(int a[], int n) { int x, i, j;

for (i = 1 ; i <= n - 1; i++) { x = a[i] ; j = i; while(j > 0 && a[j-1] > x) { a[j] = a[j-1];

j = j - 1; } a[j] = x; } //end of for i loop } // end of insertionsort

void main() { int a[100],i,n; printf("Enter number of elements "); scanf("%d" , &n);

//scan array from 0 to n-1 locations for (i = 0 ; i <= n-1 ; i++) { printf("Enter element %d " , (i+1)); scanf("%d" , &a[i]); }

//sort the array

insertionsort(a,n);

//print the sorted array for ( i=0 ; i <= n-1 ; i++) printf("%d " , a[i]);

} //end of main

 Example :

This sorting technique requires n-1 passes to sort an array of n integers. Insertion sort starts with an assumption that the array is divided into 2 partitions, sorted partition and unsorted