0% found this document useful (0 votes)
83 views40 pages

Unit 2 Serching and Sorting

Uploaded by

gp8376716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views40 pages

Unit 2 Serching and Sorting

Uploaded by

gp8376716
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

UNIT II-SERCHING AND SORTING

Chapter 2 : Searching and sorting


(MARKS-12)
__________________________________________________________________________________________
-Searching is a technique that helps to find place of a given element in DS
-Searching algorithm

Characteristics of searching algorithm:


-Efficient
-Less number of computation should be there
-Space occupied should be less

Commonly used searching algorithms are:

1. Linear or sequential search:


Linear comparison
2. Binary search :
key element (middle element) Comparison
Sequential/ linear search:
1. Sequential/ linear search:

Definition: “It is a technique in which the given list of elements is


scanned from the beginning”.

Q. Describe working of linear search with example. 4M


Ans :
- In linear search, search element is compared with each element from the list
in a sequence.
- Comparison starts with first element from the list and continues till number
is found or
- Comparison reaches to the last element of the list.
- As each element is checked with search element, the process of searching
requires more time.
- Time complexity of linear search is O (n) where n indicates number of
elements in list.
- Linear search on sorted array:-On sorted array search takes place till element
is found or
- Comparison reaches to an element greater than search element.

Example 1 :- Using array representation


Input list 10, 20, 30, 40, 50 and Search element 30, start Index =0
Sandip Polytechnic,Nashik
1
UNIT II-SERCHING AND SORTING

Iteration 1

10 20 30 40 50

10 ! = 30
Index = Index + 1
Iteration 2
10 20 30 40 50

20 ! = 30
Index = Index + 1
Iteration 3
10 20 30 40 50

30 = 30
Number found

Ex: search x= 20 in an array

a[]={13,15,18,20,25}

Algorithm for Linear Search:


Step 1: start
Step 2: read the search element from user:
Step 3: compare, the search element with first element
Step 4: if both are matching, element found
Step 5: if both are not matching, compare search element with next
Element.
Step 6: Repeat step 4, 5 till element searched/found
Step 7: If the last element doesn’t match with search element
“Element not available.”

Sandip Polytechnic,Nashik
2
UNIT II-SERCHING AND SORTING

Example: array[]
Index 0 1 2 3 4 5 6 7
elements 20 35 15 5 12 27 10 3
search element:5
step 1: start
step 2 : Read value of search element : consider integer variable x=5
step3 : compare 5 with first element in list/array i.e. 20 35 15 5 12 27 10 3
Both(search element and first element) are not matching
Step4: compare 5 with second element in list/array
20 35 15 5 12 27 10 3
Both r not matching
Step 5: compare
20 3515 5 12 27 10 3
Step 6: compare 5 with 5
Both are matching
Print message “element found at index 3.”
Step 7: STOP

PROGRAM: Write a program to search element using linear search

#inlclude<stdio.h>
#include<conio.h>
void main()
{
int search;
int a[10]={20, 35 ,15, 5 ,12, 27, 10 ,3 };
printf(“Enter element to search:”);
scanf(“%d”,&search);
Sandip Polytechnic,Nashik
3
UNIT II-SERCHING AND SORTING

for(i=0;i<10;i++)
{
if(search==a[i]) a[3]

{
printf(“Element found at location %d in array”, i);
break;
}
}
if (i==10)
printf(“Element not available in list/array”);
} // 5== a[0]20

 ADVANTAGES OF LINEAR SEARCH:

-Simple as it works in Sequential manner

-applicable to any type of list (sorted or unsorted)

 DISADVANTAGE of LINEAR SEARCH:

Applicable to small list

Binary search:
2. Binary search:
- “It is a searching technique which is applied on sorted list/Data
structure.”
- In this technique searching takes place with the help of Middle
element.
- Compare element to be search with mid element
- If element to search is less than mid element then search in
sublist 1/left sublist of the middle element
- If element to be search is greater than mid element then
search(Binary search) in sublist 2/right sublist from the middle
element.

Sandip Polytechnic,Nashik
4
UNIT II-SERCHING AND SORTING

Que. Explain the working of Binary search with an example.


- Binary search is performed only on sorted array.
- Search method starts with calculating mid position from an array and
compare the mid position element with the search element.
- If a match is found then the search process ends otherwise divide the input
list into 2 parts.
- First part contains all then numbers less than mid position element and
second part contains all the numbers greater than mid position element.
- Then select one of the part depending on search element is less or greater
than mid position element and calculates mid position for selected part.
- Again compare mid position element with search element.
- The binary search performs
Comparison and division task
The element is found
Or
Division of list gives one element for comparison.
- To calculate mid element perform (low + high) / 2.
- Low- lower index position of an array(initially 0)
- High - upper index position of an array(initially size-1)

Example 1: 20, 35 ,15, 5 ,12, 27, 10 ,3


Search element =5
Arrange Element in ascending order:

3, 5, 10, 12, 15, 20, 27, 35


Iteration 1:
Low mid high
Index 0 1 2 3 4 5 6 7
3, 5, 10, 12, 15, 20, 27, 35

Sublist1 sublist 2

select key element which is the middle element of list


Middle= (Low+high)/2; i.e 0+7/2=3.5
Element at Middle position =12
- Search element < middle element i.e. 5<12
Sandip Polytechnic,Nashik
5
UNIT II-SERCHING AND SORTING

Iteration 2: Apply binary search on sublist1


3, 5, 10

Middle= 5 search element=5

-compare middle with search element

Search element = middle element

So element found in list at location 1. In array.

Algorithm
Step 1: Start
Step 2 :Read list of elements
Step3 : assign low=0,high=n
Step 4: find middle element with (low+high)/2
Step 5: Compare element to be searched with middle element
Step 6: if(search_element<middle_element)
Then
Low=0 , high =(mid-1)
Else if(search_element=middle_element)
“Element found”
Else
Low=mid+1, high=n; mid=(low+high)/2 i.e. sublist 2
Step 7: repeat step 5 & 6 till element found or all possible sub lists
checked

Example 2:
An array which is given A[ ]= {11,5,21,3,29,17,2,43} is not in sorted manner,
first we need to sort them in order;
So an array will be A[ ]={2,3,5,11,17,21,29,43} and the value to be searched is
VAL = 29.
The binary search algorithm will proceed in the following manner

Iteration 1:
low = 0, high = 7, MID = (0 + 7)/2 = 3
Now, VAL = 29 (element to be searched) and A[MID] = A[3] =11

Sandip Polytechnic,Nashik
6
UNIT II-SERCHING AND SORTING

A[3] is less than VAL, therefore, we now search for the value in the second
half
of the array.
So, we change the values of low and MID.
Iteration 2:
Now, low = MID + 1 = 4, high = 7,
MID = (4 + 7)/2 =11/2 = 5;
VAL = 29 and A [MID] = A [5] = 21

A[5] is less than VAL, therefore, we now search for the value in the second half
of the segment.
So, again we change the values of low and MID.
Iteration 3:
Now, low = MID + 1 = 6, high = 7,
MID = (6 + 7)/2 = 6 Now,
VAL = 29 and A [MID] = A [6]=29

So, we found element 29 at location 6 in given array.

Advantages of Binary search:

-Applicable to large list

-Faster than linear search

Disadvantage of Binary search:

- Applicable to sorted data/NOT SUITABLE FOR UNSORTED


- INEFFICIENT FOR SMALL LIST

 Difference between Linear search and Binary search


Q. Differentiate Linear search and Binary search
4M

Q. Differentiate with respect to following parameters:


1. Algorithm approach 2. Complexity

Parameters Linear search Binary search


Sandip Polytechnic,Nashik
7
UNIT II-SERCHING AND SORTING

Working Works on Works on Sorted list


mechanism unsorted/sorted list
Efficient for Small list Efficient for large list
Algorithm Sequential Divide and conquer
approach
Efficiency Less efficient More efficient
Complexity Less complex More complex
Time Search time O(n) Search time :O(logn)
complexity
Speed low high
Also called Sequential Also called half-interval
search search

 Sorting :
Technique/operation of arranging data in ascending or descending
order is called sorting.
i. Internal :
which is applied on data present on main memory
Ex: 1.Bubble sort:

2 Selection sort:

3 Quick sort
4 Radix sort

ii.External sorting:
which applied on data not completely available main memory
5 Merge sort
Bubble sort:

- Bubble sort is a simple sorting algorithm. This sorting algorithm is


comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order.

Sandip Polytechnic,Nashik
8
UNIT II-SERCHING AND SORTING

- This algorithm is not suitable for large data sets as its average and worst
case complexity is of Ο(n2) where n is the number of items.

Example 1:

Consider List/ array : 33, 15, 10, 12, 25, 20, 27, 3
Pass 1: compare 33 & 15
15 33 10 12 25 20 27 3
Compare 33 & 10
15 10 33 12 25 20 27 3
Compare 33 & 12
15 10 1233 25 20 27 3
Compare 33 & 25
15 10 12 25 33 2 0 27 3
Compare 33 & 20
15 10 12 25 20 33 27 3
Compare 33 & 27
15 10 12 25 20 27 33 3
Compare 33 & 3
15 10 12 25 20 27 3 33
Pass 2: compare 15 & 10
10 15 12 25 20 27 3 33
compare 15 & 12
10 1215 25 20 27 3 33
compare 15 & 25
10 12 15 25 2027 3 33
Compare 25 & 20
10 12 15 20 25 27 3 33
Compare 25 & 27
10 12 15 20 25 27 3 33
CompRE 27 & 3
10 12 15 20 25 3 27 33
Compare 27 & 33
10 12 15 20 25 3 27 33
Pass 3 :
10 12 15 20 3 25 27 33
Sandip Polytechnic,Nashik
9
UNIT II-SERCHING AND SORTING

10 12 15 20 3 25 27 33
Pass 4 :
10 12 15 20 3 25 27 33

Compare 20 & 3:
10 12 15 3 20 25 27 33

Pass5 :
10 12 15 3 20 25 27 33

Compare 15 & 3:
10 123 15 20 25 27 33
Pass 6:
Compare 12 & 3:
10 3 12 15 20 25 27 33
Pass 7:
Compare 10 & 3:
3 10 12 15 20 25 27 33

Sorted list: 3 10 12 15 20 25 27 33

Q. Describe working of bubble sort with example.4 M


Ans
- Bubble sort is a simple sorting algorithm.
- This sorting algorithm is comparison-based algorithm in which each
pair of adjacent elements is compared and the elements are swapped if
they are not in order.
- This algorithm is not suitable for large data sets as its average and worst
case complexity is of Ο (n2) where n is the number of items.
Bubble Sort Working:
Example 2:
We take an unsorted array for our example as
A[]={19, 2, 27, 3, 7, 5, 31}. Bubble sort takes
Ο(n2) time so we're keeping it short and precise.
{{**Note: Pass 4 onwards optional**}}
Pass 1: 2,19,27,3,7,5,31
2,19,27,3,7,5,31
2,19,3,27,7,5,31
2,19,3,7,27,5,31
Sandip Polytechnic,Nashik
10
UNIT II-SERCHING AND SORTING

2,19,3,7,5,27,31
Pass 1 Completed
Pass 2: 2,19,3,7,5,27,31
2,3,19,7,5,27,31
2,3,7,19,5,27,31
2,3,7,5,19,27,31
2,3,7,5,19,27,31
Pass 2 Completed
Pass 3: 2,3,7,5,19,27,31
2,3,7,5,19,27,31
2,3,5,7,19,27,31
Pass 3 Completed
Pass 4: 2,3,5,7,19,27,31
Pass 4 Completed
Pass 5: 2,3,5,7,19,27,31
Pass 5 Completed
Pass 6: 2,3,5,7,19,27,31
Pass 6 Completed

Q. Sort the following numbers in ascending order using Bubble sort.


Given numbers: 29, 35, 3, 8, 11, 15, 56, 12, 1, 4, 85, 5 & write the
output after each interaction.
6M
Pass 1

Enter no of elements: 12

Enter array elements: 29 35 3 8 11 15 56 12 1 4 85 5

Unsorted Data: 29 35 3 8 11 15 56 12 1 4 85 5

After pass 1 : 29 35 3 8 11 15 56 12 1 4 85 5
After pass 1 : 29 3 35 8 11 15 56 12 1 4 85 5
After pass 1 : 29 3 8 35 11 15 56 12 1 4 85 5
After pass 1 : 29 3 8 11 35 15 56 12 1 4 85 5
After pass 1 : 29 3 8 11 15 35 56 12 1 4 85 5
After pass 1 : 29 3 8 11 15 35 56 12 1 4 85 5
After pass 1 : 29 3 8 11 15 35 12 56 1 4 85 5
After pass 1 : 29 3 8 11 15 35 12 1 56 4 85 5
Sandip Polytechnic,Nashik
11
UNIT II-SERCHING AND SORTING

After pass 1 : 29 3 8 11 15 35 12 1 4 56 85 5
After pass 1 : 29 3 8 11 15 35 12 1 4 56 85 5
After pass 1 : 29 3 8 11 15 35 12 1 4 56 5 85

Pass 2

After pass 2 : 3 29 8 11 15 35 12 1 4 56 5 85
After pass 2 : 3 8 29 11 15 35 12 1 4 56 5 85
After pass 2 : 3 8 11 29 15 35 12 1 4 56 5 85
After pass 2 : 3 8 11 15 29 35 12 1 4 56 5 85
After pass 2 : 3 8 11 15 29 35 12 1 4 56 5 85
After pass 2 : 3 8 11 15 29 12 35 1 4 56 5 85
After pass 2 : 3 8 11 15 29 12 1 35 4 56 5 85
After pass 2 : 3 8 11 15 29 12 1 4 35 56 5 85
After pass 2 : 3 8 11 15 29 12 1 4 35 56 5 85
After pass 2 : 3 8 11 15 29 12 1 4 35 5 56 85
Pass 3

After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 29 12 1 4 35 5 56 85
After pass 3 : 3 8 11 15 12 29 1 4 35 5 56 85
After pass 3 : 3 8 11 15 12 1 29 4 35 5 56 85
After pass 3 : 3 8 11 15 12 1 4 29 35 5 56 85
After pass 3 : 3 8 11 15 12 1 4 29 35 5 56 85
After pass 3 : 3 8 11 15 12 1 4 29 5 35 56 85

Pass 4

After pass 4 : 3 8 11 15 12 1 4 29 5 35 56 85
After pass 4 : 3 8 11 15 12 1 4 29 5 35 56 85
After pass 4 : 3 8 11 15 12 1 4 29 5 35 56 85
After pass 4 : 3 8 11 12 15 1 4 29 5 35 56 85

After pass 4 : 3 8 11 12 1 15 4 29 5 35 56 85
After pass 4 : 3 8 11 12 1 4 15 29 5 35 56 85
After pass 4 : 3 8 11 12 1 4 15 29 5 35 56 85
After pass 4 : 3 8 11 12 1 4 15 5 29 35 56 85

Sandip Polytechnic,Nashik
12
UNIT II-SERCHING AND SORTING

Pass 5

After pass 5 : 3 8 11 12 1 4 15 5 29 35 56 85
After pass 5 : 3 8 11 12 1 4 15 5 29 35 56 85
After pass 5 : 3 8 11 12 1 4 15 5 29 35 56 85
After pass 5 : 3 8 11 1 12 4 15 5 29 35 56 85
After pass 5 : 3 8 11 1 4 12 15 5 29 35 56 85
After pass 5 : 3 8 11 1 4 12 15 5 29 35 56 85
After pass 5 : 3 8 11 1 4 12 5 15 29 35 56 85

Pass 6

After pass 6 : 3 8 11 1 4 12 5 15 29 35 56 85
After pass 6 : 3 8 11 1 4 12 5 15 29 35 56 85
After pass 6 : 3 8 1 11 4 12 5 15 29 35 56 85
After pass 6 : 3 8 1 4 11 12 5 15 29 35 56 85
After pass 6 : 3 8 1 4 11 12 5 15 29 35 56 85
After pass 6 : 3 8 1 4 11 5 12 15 29 35 56 85

Pass 7

After pass 7 : 3 8 1 4 11 5 12 15 29 35 56 85
After pass 7 : 3 1 8 4 11 5 12 15 29 35 56 85
After pass 7 : 3 1 4 8 11 5 12 15 29 35 56 85
After pass 7 : 3 1 4 8 11 5 12 15 29 35 56 85
After pass 7 : 3 1 4 8 5 11 12 15 29 35 56 85

Pass 8

After pass 12 :1 3 4 8 5 11 12 15 29 35 56 85

Sorted elements are 1 3 4 8 5 11 12 15 29 35 56 85

 ALGORITHM
Algorithm: Let consider Array[ ] is an array, N is size of array

Step 1: start
Sandip Polytechnic,Nashik
13
UNIT II-SERCHING AND SORTING

Step 2 : Repeat for pass 1 to N-1=pass


Step3: Repeat for i=0 to n-1
Step:4 compare two elements:
If Array[i] > Array[i+1]
Step 5: Interchange Array[i] with Array[i+1]
Step 6: Print sorted Array

Q. Write a programm to sort list of elements using bubble sort Algorithm


Programm:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int A[10], n,i, pass, j ,temp=0;
printf(“Enter value of n –size of array:”);
scanf(“%d”,&n);

printf(“Enter n elements :\n”);


for(i=0,i<n,i++)
{
scanf(“%d”, &A[i]);
}

for(pass=1;pass<=n-1;pass++)
{
for(j=1; j<n-pass;j++)
{
If (A[j]>A[j+1])
{
temp=A[j+1];
A[j+1]=A[j];
A[j]=temp;
}
}
}
printf(“\n Sorted array is:”);
for(i=0;i<n;i++)
{
Printf(“%d”, A[i])
Sandip Polytechnic,Nashik
14
UNIT II-SERCHING AND SORTING

}
}

Advantages of bubble sort:


1. The primary advantage of the bubble sort is that it is popular and
easy to implement.
2. Elements are swapped in place without using additional temporary
storage, so the space requirement is at a minimum.

Disadvantages
1. Bubble sort does not deal well with a list containing a huge number
of items.
This is because the bubble sort requires n-squared processing steps for
every n number of elements to be sorted.
------------------------------------------
Selection Sort:
-Selection Sort algorithm is used to arrange a list of elements in a
particular order (Ascending or Descending).
In selection sort, during pass 1: smallest element from the list get
selected and exchanged with first element in the list.
During pass 2: smallest element from the remaining list get selected
and exchanged with second element from list.
This procedure is repeated till the entire list is sorted.

- List get divided into two parts- sorted list & unsorted list

Q. Describe working of selection sort method.


Also sort given input list in ascending order using selection sort
input list:- 55, 25, 5, 15, 35.

 Working of Selection sort:

Selection Sort algorithm is used to arrange a list of elements in a particular


order (Ascending or Descending).
In selection sort, during pass 1: smallest element from the list get selected and
exchanged with first element in the list.
During pass 2: smallest element from the remaining list get selected and
exchanged with second element from list.
This procedure is repeated till the entire list is sorted.
Sandip Polytechnic,Nashik
15
UNIT II-SERCHING AND SORTING

Sandip Polytechnic,Nashik
16
UNIT II-SERCHING AND SORTING

Algorithm:

Step 1: START
Step 2: Select first element from array or list
Step 3: Compare selected element with all other elements
Step 4: for every comparison if any element is smaller than selected
element then swap two elements.
Step 5: Repeat step 2 to 4 till we get sorted list.
OR
consider Array[], N size

step 1 : START
step 2 : Repeat for i=1 to N-1
step 3 : set Min=Array[i] and loc=i
step 4:repeat for j=i+1 to N-1
step 5: If Min > Array[j]
Then
Set Min =Array [j] and loc=j
Step 6: Interchange Array[i] with Array[loc]
Step 7: STOP

Example 1. : 23 41 11 32 40
Pass 1: i/p: 23 41 11 32 40
o/p: 11 41 23 32 40
Pass 2 : i/p: 11 41 23 32 40
o/p: 11 23 41 32 40
Pass 3: i/P: 11 23 41 32 40
o/p: 11 23 32 41 40
Pass 4: i/p:11 23 32 41 40
o/p: 11 23 32 4041
sorted list is : 11 23 32 40 41

Sandip Polytechnic,Nashik
17
UNIT II-SERCHING AND SORTING

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j ,loc,extra, Array[],temp,n;
printf(“Enter size of array:”);
scanf(“%d”,&n);

printf(“\n Insert %d elements”,n);


for(i=0;i<n;i++)
{
Scanf(“%d”,&Array[i]);
}

for(i=0;i<n-1;i++)
{
temp = Array[i];
loc=I;

for(j=i+1; j<n;j++)
{
If(temp>Array[j])
{
temp = Array[j];
loc=j;
}
}
Extra=Array[i];
Array[i]=Array[loc];
Array[loc]=Extra;
}

printf(“\n Sorted array is :\t”,);

for(i=0;i<n;i++)
{
Sandip Polytechnic,Nashik
18
UNIT II-SERCHING AND SORTING

printf(“\t %d”, Array[i]);


}

Advantages of selection sort


 Easy to implement
 Performance is improved compare to bubble sort

Disadvantage :

1. Not so good for large data

--------------------------------

Insertion sort:
- It is another kind of sorting technique because it starts with
index 1 instead of index 0 .

How it works?

Comparison insertion sort starts with 1st position (2 element in array)


Pass1: -2nd element compared with 1st element.
If 2nd is less than 1st then insert 2nd at position/index 0
Pass 2/iteration 2:
Third element get compared with 2nd element then compare with 1st
element and then place that element at proper position
Iteration 3:
4th element will get compared with 3rd 2nd 1st element
/this procedure repeated till complete list get sorted/ comparisons takes
place upto last element in the list

Sandip Polytechnic,Nashik
19
UNIT II-SERCHING AND SORTING

EXAMPLE : 23 41 11 32 40
Pass 1: compare 41 with 23
41 is at proper place
So after pass1 array :
23 41 11 32 40
Pass 2 : Compare 11 with 41 & 23
Here 11 is less than 23 so insert 11 before 23
After pass 2: 11 23 41 32 40
Pass 3: Compare 32 with 41 23 11
Insert 32 before 41
After pass 3 : 11 23 32 41 40
Pass 4 : Compare 40 with 41 32 23 11
40 is less than 41 so shift it before 41
After pass 4: 11 23 32 40 41

Example 2:
Elaborate the steps for performing insertion sort for given elements of array.
30 10 40 50 20 45
We take given array:

Iteration 1:
Insertion sort compares the first two elements:
It finds that 30 and 10 are not in sorted order; so it will swap 30 with 10; and the resultant
sorted sub list will be
10 30 40 50 20 45

Iteration 2:
Now it will check for third element; It will first checks 40 with first element that is 10 which is
less than 40; then it will check 40 with 30 and concludes that 30 is lesser than 40 so eventually
it finds that 40 is already in its desirable location so 40 will be added to sub list.
10 30 40 50 20 45

Iteration 3:
In Iteration 3 it will check for fourth element which is 50 which will be compare with 10; it
finds that 10 is smaller than 50, then it will check next element is sub list, which is 30, this new

Sandip Polytechnic,Nashik
20
UNIT II-SERCHING AND SORTING

element is again smaller than 50, so it will consider next element for comparison which is 40.
This element is also smaller so it will keep 50 to its place.
10 30 40 50 20 45

Iteration 4:
Next it will check for fifth element which is 20 with first element which is 10 so it will jump to
next location. At location two it will check with the element 30 with 20 which is greater than
20. So it will shift all elements 30 onwards by one position and inserts 20 at desired location.

10 30 40 50 20 45
10 20 30 40 50 45

Iteration 5:
Now it will check for sixth element which is 45 it is observed as greater than 10 so it will
consider next element for comparison which is 20. After comparison with 20 it will compute
that 20 is smaller than 45 and hence at its appropriate place. Further it will select next
location’s element for comparison which is 30, this element is also smaller than 45 hence next
element will be selected. After comparing next element which is 40 and smaller than 45 last
element will be consider. The last element which is 50 and also greater than 45 will be shifted
by one place and will give list in sorted order.
10 20 30 40 50 45
10 20 30 40 45 50

 Advantages and disadvantages :


1. Memory requirement is less “in place sorting algorithm”

Disadvantage:
- It best applicable to small list
- Inefficient for Large list

Sandip Polytechnic,Nashik
21
UNIT II-SERCHING AND SORTING

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j , temp ,n, Array[10];
printf(“Enter size of list/array:”);
scanf(“%d”, &n);
printf(“\n Enter Array Elements:”);
for(i=0;i<n;i++)
{
scanf(“%d”,&Array[i]);
}
for(i=1;i<n;i++)
{
temp=Array[i];
j=i-1;
while((temp<Array[j])&& j>=0)
{
Array[j+1]=Array[j];
j=j-1;
}
Array[j+1]=temp;
}
printf(“\n Sorted Array is:”);
for(i=0;i<n;i++)
{
printf(“%d”,Array[i]);
}
}
---------------------------------

4. Quick sort :
Sandip Polytechnic,Nashik
22
UNIT II-SERCHING AND SORTING

-Quick sort uses ‘Divide and conquer”.


-It is highly efficient sorting algorithm and it is based on partitioning
of array/list
-Quick sort first select a value which is called as ‘Pivot’
-Pivot-one selected element from list -lowest position element.
-Role of pivot is assist to divide the list.
How it works?
-A large array is partitioned into two arrays one of which holds values
smaller than pivot and another array holds values greater than pivot.
This partitioning takes place with the help of Pivot.
-Quick sort partitions the list/Array and then calls recursively twice to
sort complete list.
-this algorithm first selects an element as pivot(At lowest index or
Highest index).
-Compare pivot with elements in list start from left to right one by one
If element<=pivot
Increment index
Else stop incrementing, jump to element
At highest index,
Then compare pivot with element at highest index.
If element>pivot
Then
Decrement index
Else
Swap values at indexes stopped at left and right.
Continue the procedure till we get complete array sorted.

 Algorithm:
Consider Array[] is list, low is starting index, high is highest index
Quick_sort(Array[],low,high)
{
Pivot=low;
i=low;
j=high;
Sandip Polytechnic,Nashik
23
UNIT II-SERCHING AND SORTING

while(i<j)
while ((array[i]<=pivot )&& (i<high)
i++;
while (Array[j]>pivot)
j--;
if(i<j)
{
temp=array[i];
array[i]=array[j]; swapping of elements at
elements at index I & j
array[j]=temp;
}
temp=array[pivot]; swapping of elements at
elements at pivot& j
array[pivot]=array[j];
array[j]=temp;
}
Quick_sort(array,low,j-1);
Quick_sort(array,j+1,high);

i,j,i = to lowest
J= highest index
I=1 if value at index I is less than pivot
increase value of i .
I=2
I=3 if value at index i is greater than pivot value
Compare pivot with element at index j
Element at jth index may be smaller or greater
If element at j th is smaller than pivot then interchange values at
position i& j
If value at jth index is greater, decrement value of j.
Sandip Polytechnic,Nashik
24
UNIT II-SERCHING AND SORTING

-partitions and arrangement of element s takes place

ExAMPLE 1 : Sort the Array : 50 30 10 90 80 20 40 70 using Quick


sort
Ans:
Consider the given list as following array indications
low high
Array : 50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Let, Pivot=Array[0],i=0, j=7
Step1 :
Pivot=Array[0], i=0, j=7
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
i/pivot j
compare element at i=0 with pivot (i.e 50)
Array[i] i.e. Array[0] <=pivot,
increase value of i

Step 2: Pivot=Array[0], i= 1 , j=7

50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Compare element at indexi=1 (Array[1]) with pivot
Array[1]<=pivot i.e.30<50
Increase i

Step 3: Pivot=Array[0], i= 2 , j=7

50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j

Sandip Polytechnic,Nashik
25
UNIT II-SERCHING AND SORTING

Array[i]=10
Compare Array[i]i.e.Array[2] with pivot (50)
Array[2]<=pivot i.e 10 <50
Increase i
Step 4: Pivot=Array[0], i= 3 , j=7

50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Compare Array[3]i.e 90 with pivot (50)
Array[3]>pivot i.e 90 > 50
Stop incrementing i
Step 5 : jump to index j, j=7
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
compare Array[j]i.eArray[7] with pivot
Array[7]>pivot i.e. 70>50
Then decrement value of j
Step 6 : Pivot=Array[0], i= 3 , j=6
50 30 10 90 80 20 40 70
Index 0 1 2 3 4 5 6 7
Pivot I j
Compare Array[j] i.eArray[6]=40 with pivot =50
Array[6]<pivot 40<50
Look at values at index i& j i.e( 90 & 40)

Interchange values at index i& j

: 50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Step 7: jump to i
Compare element at ith position with pivot i.e
Array[i] = Array[3]= 40 compare with pivot i.e 50
Array[i] <pivot i..e40<=50 ,
Sandip Polytechnic,Nashik
26
UNIT II-SERCHING AND SORTING

Increase value of i
Step 8 :Pivot=Array[0], i= 4 , j=6

50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Compare Array[i] i.e. Array[4] with pivot i.e. 50
Array[i] > pivot i.e.80>50
Stop incrementing i

Step 9 :
Jump to j
50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j

Compare Array[j]i.e. Array[6]=90 with pivot 50

Array[j] > pivot 90 >50


Decrement value of j

Step 10 :Pivot=Array[0], i= 4 , j=5

50 30 10 40 80 20 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j

Compare Array[j] i.e. Array[5]=20 with pivot 50

Array[j] <pivot 20 <50


Interchange values at index i& j

50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Step 11: jump to i

Sandip Polytechnic,Nashik
27
UNIT II-SERCHING AND SORTING

Pivot=Array[0], i= 4 , j=5
50 30 10 4020 8090 70
Index 0 1 2 3 4 5 6 7
Pivot i j
Compare Array[i] i.e. Array[4]=20 with pivot =50
Array[4]<pivot 20<50
Increment i

Step 12: Pivot=Array[0], i= 5, j=5

50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot i/j
Compare Array[i] i.e .Array[5]=80 with pivot
Array[5]>pivot 80>50
Stop incrementing i
Step 10: jump to j
Pivot=Array[0], i= 5, j=5
50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot i/j

Compare Array[j] i.e.Array[5]=80


Array[5]>pivot 80>50

Decrement j

: 50 30 10 40 20 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot j i

Now,i=5 & j=4i.ei>j

Interchange pivot element with Array[j],

20 30 10 40 50 80 90 70
Index 0 1 2 3 4 5 6 7
Pivot j i
Sandip Polytechnic,Nashik
28
UNIT II-SERCHING AND SORTING

Sublist 1 sublist 2

Step 11: Now Apply Quicksort on sublist 1

elements 20 30 10 40
Index 0 1 2 3
Pivot/i j

i=0 , j=3

pivot=Array[low]=20

Compare pivot i. e. 20 with element at indexi (i=0) Array[0]=20

Array[i] <=pivot

increment i;

step 12:
pivot =Array[0], i=1,j=3

20 30 10 40
Index 0 1 2 3
Pivot i j

Step: Compare pivot with Array[i]i.eArray[1]=30

Array[i]>pivot

Stop incrementing i

Step13: pivot =Array[0], i=1,j=3

20 30 10 40
Index 0 1 2 3
Pivot I j

Compare pivot with Array[j]=Array[3]=40

Array[3]>pivot i.e 40>20

Decrement value of j
Sandip Polytechnic,Nashik
29
UNIT II-SERCHING AND SORTING

Step14 :pivot =Array[0], i=1,j=2

20 30 10 40
Index 0 1 2 3
Pivot i j

Compare pivot with Array[j]=Array[2]=10

Array[2]<pivot
Interchange values at index i&j
20 10 30 40
Index 0 1 2 3

pivot i j

step15 :: pivot =Array[0], i=1,j=2


Compare pivot with Array[i]= Array[1]=10
Array[1]<pivot
Increment i
20 10 30 40
Index 0 1 2 3
Pivot i/ j
step16 : compare pivot with Array[i]= Array[2]=30
Array[2]>pivot
Stop incrementing i
compare pivot with Array[j]= Array[2]=30
Array[2]>pivot
Decrement j
Step17 :
20 10 30 40
Index 0 1 2 3
pivot j i
Now ,i>j Interchange pivot element with Array[j]
10 20 30 40
Index 0 1 2 3
Now, Apply Quick sort on sublist 2
80 90 70
Index 0 1 2
Sandip Polytechnic,Nashik
30
UNIT II-SERCHING AND SORTING

pivot/i j
compare pivot with Array[i]=80
increment I
80 90 70
Index 0 1 2
Pivot i j
Compare pivot Array[1]=90
Array[1]> pivot
Stop incrementing i
Comare Array[j] with pivot
Array[2]<pivot
Interchange values at index I & j
80 70 90
Index 0 1 2
Pivot I j
Compare Array[i]=Array[1]=70 with pivot
Array[1]<pivot
Increment i
80 70 90
Index 0 1 2
Pivot i/j
Compare Array[i] with pivot

Array[i]>pivot
Compare Array[j] with pivot
Array[j]>pivot
Decrement j
80 70 90
Index 0 1 2
Pivot j i

As i>j , interchange pivot & Array[j]

70 80 90

Final sorted list 10 20 30 40 50 70 80 90


Sandip Polytechnic,Nashik
31
UNIT II-SERCHING AND SORTING

Example 2 :Home work

Sort the following numbers in ascending order using quick sort. Given
numbers 50, 2, 6, 22, 3, 39, 49, 25, 18, 5

Advantages of Quick sort :

-Quick sort is best sorting algorithm

-It is easily applicable for huge list.

Disadvantage

-Implementation Partitioning is difficult


-Worst case performance of quick sort is equal to average performance of
other algorithms.

Note: Algorithm Performance:


1) Best case : when sorting takes place with minimum number of
passes.

2) Average : When sorting takes place with average passes .


3) Worst case: When sorting takes place with maximum number of
passes .

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j, Array[20],n;
printf(“Enter size of array:”):
Sandip Polytechnic,Nashik
32
UNIT II-SERCHING AND SORTING

scanf(“%d”,&n);
Printf(“%d elements:”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&Array[i]);
}
Quicksort(Array,0,n-1);
printf(“\n Sorted array is:”);
for(i=0;i<n;i++)
{
printf(“%d”, Array[i]);
}
void Quicksort(Array[20],int low, int high)
{
pivot=low;
i=low;
j=high;
while(i<j)
{
while((array[i]<=pivot) && (i<high))
{
I++;
}
While(array[j]>pivot)
{
j--;
}
If(i<j) array[j]<pivot
{
Temp=array[i];
Array[i]=array[j];
Array[j]=temp;
}
}
temp=pivot;
pivot=array[j];
array[j]=temp;
Quicksort(Array, low, j-1);

Quicksort(Array, j+1, high);


Sandip Polytechnic,Nashik
33
UNIT II-SERCHING AND SORTING

---------------------------------

1. Radix sort/Bucket Sort:

-In all previous sorting algorithms work with comparison technique.

-Radix sort works with bits/digits of numbers in list.

-It can be used for alphabets also.

-Radix sort is also called as bucket sort.

-unit place digit, tens digit, hundreds


Describe working of radix sort along with example. 4M
Ans:
Radix Sorting: - In this method, ten buckets (0-9) are used to sort elements of
an
input list. All the elements are sorted according to their digit position from
each
element.
In pass one each element is placed inside the bucket with respect its unit
position digit. After placing all elements inside the buckets, read those from
0th bucket to 9th bucket.
In pass 2, elements are placed in buckets with respect to10th position digit
from each element. In each pass one position is considered to arrange all the
elements in bucket. At the end of each pass elements are collected from
buckets and given as input to the next pass. Total number of passes required
for sorting is equal to maximum number of digits present in the largest
number from the input list. Last pass gives sorted list after reading all
elements from 0th bucket to 9th bucket.

Example1 : 18,253, 1000,2 ,80,75,58

EXAMPLE 2:

Pass 0 1 2 3 4 5 6 7 8 9
Sandip Polytechnic,Nashik
34
UNIT II-SERCHING AND SORTING

1 12 23 44 48
2 12 23 44,48
3 144 248 312 423

Algorithm:
1. Read List of elements
2. Now apply simple procedure to sort elements digit by digit.
2.1 Sort elements according to the last digit (least significant bit)
(unit place) first and place in proper bucket.
2.2 Then sort according to second last digit.
2.3 Then sort according to first/most significant bit and place
element in proper bucket.
3. Print Sorted list.
4. STOP.
OR

Algorithm:
Consider an array A[]

Step 1:start

Step 2: get largest number from array

Step 3: while(largestNum/digitplace>0)

Step 4:consider a count[]array

Step5:fori=0 to n do

Count[key A[i] in pass j]++

Step6 : For k=1 to 10 do


Sandip Polytechnic,Nashik
35
UNIT II-SERCHING AND SORTING

Count[k]=count[k]+count[k-1]

Step7: For i=n-1 down to 0

Do

Result[count[key of A[i]]]=A[j]

Count[key of A[i]] - -

Step 8: For i=0 to n do

A[i]=result[i]

Digitplace *=10

Advantages and disadvantages of Radix sort:

1. Radix sort can be used for sorting numbers as well as strings.


2. Radix sort is most efficient algorithm

Disadvantage:

1. Less flexible than other algorithms.

2. It takes more space for execution.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
Int digitplace=1;
Int A[], I,j,k, count[]
Int n;
Sandip Polytechnic,Nashik
36
UNIT II-SERCHING AND SORTING

Printf(“Enter size of array:”);


Scanf(“%d”,&n);
Printf(“Enter n elements in array”);
For(i=0;i<n;i++)
{
Scanf(“%d”,&A[i]);
}
radixSort(A,n);
Printf(“Sorted array is:”);
For(i=0;i<n;i++)
Printf(“%d”,A[i]);
}
getMax(A[],int n)
{
Int i;
Int max=A[0];
For(i=1;i<n;i++)
if(A[i]>max)
Max=A[i];
}
radixSort(A[],n)
{
Int result[],count[],
int largestNum=getMax(A,n);
While(largestNum/digitplace>0)
{
Int count[10]={0};
For(i=0;i<n;i++)
Count[(A[i]/digitplace)%10]++;
For(i=1;i<10;i++)
Count[i] +=count[i-1];//a+=a; a=a+a;
//Count[i]=count[i]+count[i-1]
For(i=n-1;i>=0;i++)
{
Result[count[(a[i]/digitplace)%10]-1]=A[i];
Count[(A[i]/digitplace)%10]- -
}
For(i=0;i<n;i++)
A[i]=result[i];
Digitplace*=10;
Sandip Polytechnic,Nashik
37
UNIT II-SERCHING AND SORTING

}
}
--------------------------------------

 MERGE SORT
Describe working of merge sort with example.
4M

Ans:
All the elements from the input list are divided into groups. Each group is
sorted
independently and merged together in each iteration. These iterations
continue
performing divide, sort and merge procedure till all elements are placed in
one
single group.
The final group can be sorted with any other sorting method to get a sorted
list.
Example:- input list: 10,1,9,11,46,20,15,0,72,2

Sandip Polytechnic,Nashik
38
UNIT II-SERCHING AND SORTING

In the above example, input list is divided into group of two elements in iteration
1 and sorted in that group. In iteration 2, two groups from iteration 1 are merged
together to form group of four elements. Again each group is sorted and merged
together in iteration 3. In iteration 3, two groups of 4 elements are merged
together to form group of eight elements. Then they are sorted and merge together
to form a group of ten elements in iteration 4. Now all these elements are in a
single group so they are sorted together with any other sorting algorithm.
Sorted list is: 0,1,2,9,10,11,15,20,46,72.

QUE. Give complexity of following methods:

i) Bubble sort
ii) Radix sort
iii) Insertion sort
iv) Selection sort
v) Quick sort
vi) Linear search
vii) Binary search.

Ans: i) Bubble Sort: O(n2)


ii) Radix sort: O(K.N)

Sandip Polytechnic,Nashik
39
UNIT II-SERCHING AND SORTING

Where K=Number of didgit in maximum number in the given array.


N=number of data element in an arrray
iii) Insertion sort: O(n2)
iv) Selection sort: O(n2)
v) Quick sort: O(n log n)
vi) Linear search: O(n)
vii) Binary search: O(log n)

**********************************Best Luck***********************************

Sandip Polytechnic,Nashik
40

You might also like