0% found this document useful (0 votes)
10 views

DSU NOTES( CH2)

The document provides an overview of sorting and searching algorithms, including Bubble Sort, Selection Sort, Quick Sort, Insertion Sort, and Radix Sort, detailing their processes, complexities, advantages, and disadvantages. It also explains searching techniques such as Linear Search and Binary Search, along with their algorithms. Additionally, it includes programming tasks related to these concepts for practical understanding.

Uploaded by

swaralibarve831
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)
10 views

DSU NOTES( CH2)

The document provides an overview of sorting and searching algorithms, including Bubble Sort, Selection Sort, Quick Sort, Insertion Sort, and Radix Sort, detailing their processes, complexities, advantages, and disadvantages. It also explains searching techniques such as Linear Search and Binary Search, along with their algorithms. Additionally, it includes programming tasks related to these concepts for practical understanding.

Uploaded by

swaralibarve831
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/ 15

COURSE CODE: 313301

CH-2 SORTING & SEARCHING

Sorting:

➢ Sorting is storage of data in sorted order which can be either in ascending or


descending order.
➢ If data is kept in some sorted order, then searching becomes very easy.
➢ Sorting is divided into 2 categories:

Bubble Sort:

➢ In this method, to arrange elements in


ascending order, begin with the 0th
element, which is compared with the
1st element.
➢ If it is found to be greater than the 1st
element then they are interchanged.
➢ Then the 1st element is compared with
the 2nd element, if it is found to be
greater, then they are interchanged.
Eg: ➢ In the same way all the
elements are compared with
their next element & are
interchanged if required.
➢ This is the first iteration & process will
be repeated till list gets sorted and
after each iteration the largest element
gets placed at the last.
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps
since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.

Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )

Complexity:
Best Case O(n)
Average Case O(n^2)
Worst Case O(n^2)

Advantages

➢ The bubble sort requires very little memory other than that which the array or
list itself occupies.
➢ The bubble sort consists of relatively few lines of code.
➢ 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.
➢ The same applies for data sets that have only a few items that need to be
swapped a few times.

Disadvantages

➢ The main disadvantage of the bubble sort method is the time it requires.
➢ With a running time of O (n^2), it is highly inefficient for large data sets.
Program to implement Bubble Sort:
#include <stdio.h>
void main()
{
int array[10], n, i, j, temp;
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]);
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]);
getch();
}

Selection Sort:

➢ This is the simplest method of sorting.


➢ In this method to sort the data in ascending order, o th elements are compared
with all other elements.
➢ If the 0th element is found to be greater than the compared element then
they are interchanged.
➢ So after the 1st iteration, the smallest element is placed at 0th position.
➢ The same procedure is repeated for all elements.
➢ Eg:
Consider the following depicted array as an example.
➢ For the first position in the sorted list, the whole list is scanned sequentially. The
first position where 14 is stored presently, we search the whole list and find that 10
is the lowest value.
➢ So we replace 14 with 10. After one iteration 10, which happens to be the minimum
value in the list, appears in the first position of the sorted list.
➢ For the second position, where 33 is residing, we start scanning the rest of the
list in a linear manner.
➢ We find that 14 is the second lowest value in the list and it should appear at
the second place. We swap these values.

➢ After two iterations, two least values are positioned at the beginning in a sorted
manner.

➢ The same process is applied to the rest of the items in the array.
Fig: Selection Sort

Quick Sort:

➢ Quick sort can sort a list of data


elements significantly faster than any
other sorting algorithm.
➢ In quick sort, the original list is divided into
2 sublists.
➢ We choose the item from the list called
key or pivot from which all the left side
elements are smaller and all the right
side elements are greater than that
element.
➢ Process for sorting elements:
○ Take first element of list as pivot
Eg:
○ Place pivot at the proper
place in the list, such that all
elements at its left are
smaller and all elements at
its right are greater.
○ Create two sublists left and right
side of pivot.
○ Repeat the same process until all
elements of the list are at the proper
position in the list.
Insertion Sort:

➢ Insertion sort is implemented by inserting a particular element at the


appropriate position.
➢ In this method, the first iteration starts with comparison of the 1st element
with oth element.
➢ In the second iteration, THE 2ND element is compared with the 0th & 1st element.
➢ In general, in every iteration an element is compared with all elements before it.
➢ During comparison if it is found that the element can be inserted at suitable
position then space is created for it by shifting the other elements one position
to right & inserting element at suitable position.
➢ This procedure is repeated for all the elements in array:
➢ Eg:

Radix Sort:

➢ Radix sort is based on values of the actual


digits in the positional representation.
➢ To sort decimal numbers, where the radix
or base is 10, we need 10 buckets.
➢ These buckets are numbered from 0,1,2,
9.
➢ Number of passes required to sort
using radix sort is equal to the
number of digits in the largest
number in the list.
Eg: ➢ In the first pass, numbers are sorted
on least significant digit. Numbers
with the same least significant digit are
stored in the same bucket.
➢ In the 2nd pass, numbers are sorted on the
second least significant digit.
➢ At the end of every pass, numbers in
buckets are merged to produce a common
list.
Consider the following numbers:
348, 14, 614, 5381, 47
number 0 1 2 3 4 5 6 7 8 9

348 348

14 14
614 614

5381 5381

47 47
After Pass 1: 5381, 14, 614,47, 348
Pass 2:

number 0 1 2 3 4 5 6 7 8 9

5381 5381

14 14

614 614

47 47

348 348
After Pass2: 14, 614, 47, 348, 5381

Pass 3:
number 0 1 2 3 4 5 6 7 8 9

14 14

614 614

+47 47

348 348

5381 5381
After pass 3: 14,47, 348, 5381,614

Pass 4:
number 0 1 2 3 4 5 6 7 8 9

14 14

47 47

348 348
5381 5381

614 614
After pass 4: 14,47,348,614, 5381
Searching:

➢ Searching is an operation which finds the location of a given element in a list.


➢ Search is said to be successful or unsuccessful depending on whether the element
that is to be searched is found or not.

Linear Search:

➢ This is the simplest method of searching.


➢ In this method, the element to be found is sequentially searched in the list.
➢ We have to start the search from the beginning and scan the elements one by
one until the end of the array.
➢ If the search is successful, then it will return the location of the element
otherwise it will return a failure notification.

Algorithm:

1) Start
2) Accept elements from user
3) Repeat for i=0 to n-1
If a[i]=element then
Element found at location i+1
Exit Else
Element not found

4) stop

Binary search:

➢ Binary search is very fast and efficient


➢ This method requires that the list of elements should be in the sorted order
➢ In this method to search an element, we compare it with the element present at the
middle of the list.
➢ If it matches then the search is successful
➢ Otherwise the list is divided into two half from 0th element to center element and
second from center element to last element
➢ Now the searching will proceed depending upon whether the element is greater or
smaller than the center element.
➢ If element is smaller than center element searching will be done in first
half, otherwise in the second half.
➢ This procedure is repeated till the element is found.
For eg
➢ Consider the following elements :
10, 18, 19, 20, 25, 30, 49, 57, 64,72
➢ And searching element is 57
➢ Pass I:
S
t
a
r
➢ Pass 2: t
=
0
E
n
d
=
9
M
i
d
=
(
s
t
a
r
t
+
e
n
d
)
/
2
=
4
5
7
>
2
5
M
S i
t d
a =
r (
t 9
= +
m 5
i )
d /
+ 2
1 =
= 7
5 5
E 7
n =
d 5
= 7
9

MSBTE Questions:

1) Write a program for selection sort and arrange the given numbers in
ascending order using selection sort(16, 23, 13, 9, 7,5)
2) Explain binary search.
3) Sort the given numbers in ascending order using radix sort(348, 14,
614, 5381, 47)
4) Write a c program for binary search.
5) Explain linear search algo.
6) Wap to sort an array of 10 elements by using bubble sort.
7) Describe bubble sort with help of example. Take minimum 4 values.
8) Describe principle of selection sort with example.

You might also like