Unit 2 Notes
Unit 2 Notes
❖ Sorting:
Definition:
● Sorting is an operation which we can perform on the data structure to arrange the elements in
ascending and descending order.
● Sorting is the process where row data takes as input and generate the ordered data, it may be in
ascending or descending order.
● Sorting is the operation of arranging data or elements in some given order.
Classification of sorting:
1. Internal Sorting: The sorting which is done on computer main memory is known as internal
sorting. In this sorting technique, all the data is stored in main memory only and the data can
be accessed randomly. Example: Bubble sort, Selection sort, Insertion sort, Quick sort, Radix
sort, etc.
2. External Sorting: The sorting which is done on secondary memory is known as external
sorting. If number of elements to be sorted is too large then external sorting is required.
Example: Merge sort, etc.
❖ Sorting Techniques:
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Radix Sort
5. Quick Sort
6. Merge Sort
7. Shell Sort
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
Chapter 02 Searching and Sorting VJTech Academy
❖ Bubble Sort:
● Bubble sort is the one of the type of sorting algorithm. It is stable sorting algorithm.
● Following are the steps involved in the Bubble sort:
1. Compares two adjacent elements.
2. If current element is greater than next element then swap them.
3. If current element is less than next element then no action required, move to the next element
and repeat step 1.
● Sorting begins with 0th elements & it compares with 1st element in given list.
● If 0th element is greater than 1st element then swap them. Like this all elements are compared
with next element & interchanged if required.
● At the end of 1st pass largest element is placed at last position.
● In 2nd pass again comparisons starts with 0th element & after completion of 2nd pass, 2nd largest
element is placed at second last position.
● This process continues till list is in sorted order.
● In Bubble sort, total N-1 passes are required for sorting N elements.
● Example:
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
Chapter 02 Searching and Sorting VJTech Academy
● Time complexity:
Best Case => O(n2), Average Case => O(n2) Worst Case => O(n2)
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
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;
}
}
}
printf("\n\n\n Sorting Array Elements are: ");
for(j=0;j<N;j++)
{
printf(" %d",a[j]);
}
getch();
}
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
Chapter 02 Searching and Sorting VJTech Academy
❖ Selection Sort:
● Selection sort is the one of the type of sorting algorithm.
● Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending or
Descending).
● In selection sort, the first element in the list is selected and it is compared repeatedly with
remaining all the elements in the list.
● If any element is smaller than the selected element , then both are swapped.
● At the end of 1st pass smallest element is placed at first position.
● In 2nd pass, we select the element at second position in the list and it is compared with remaining
all elements in the list.
● If any element is smaller than the selected element, then both are swapped.
● At the end of 2st pass second smallest element is placed at second position.
● This procedure is repeated till the entire list is sorted.
● In selection sort, total N-1 passes are required for sorting N elements.
● Example:
● Time complexity:
Best Case => O(n2),
Average Case => O(n2)
Worst Case => O(n2)
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
Chapter 02 Searching and Sorting VJTech Academy
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<N;i++)
{
k=i;
for(j=i+1;j<N;j++)
{
if(a[j]<a[k])
{
k=j;
}
}
if(k!=i)
{
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
}
printf("\n\n\n Sorting Array Elements are: ");
for(j=0;j<N;j++)
{
printf(" %d",a[j]);
}
getch();
}
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
Chapter 02 Searching and Sorting VJTech Academy
❖ Insertion Sort:
● Insertion sort is the one of the type of sorting algorithm. It is stable sorting algorithm.
● In insertion sort, sorting is done on the basis of shift and insert principle.
● In insertion sort, elements are inserted at right position.
● In first pass, 1st index element is compared with oth index element. If 0th index element is
greater than 1st index element then store 1st index element into a temporary variable and shift oth
index element to its right by one position. Then insert temporary variable value in oth position.
● In 2nd pass, compare 2nd index element with 1th index and then 0st index elements. If required
perform shift and insert operations.
● In each pass fix one position and compare it with all elements placed before it.
● Continue this process till last position element is compared with all elements placed before it.
● In Selection sort, total N-1 passes are required for sorting N elements.
● Example:
Refer your class notebook.
● Time complexity:
Best Case => O(n^2),
Average Case => O(n^2)
Worst Case => O(n^2)
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
Chapter 02 Searching and Sorting VJTech Academy
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
Chapter 02 Searching and Sorting VJTech Academy
❖ Radix Sort:
● Radix sort is the one of the type of sorting algorithm It is stable sorting algorithm..
● 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 to 10th 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 0 th bucket to 9th bucket.
● Time complexity:
Best Case => O(n)
Average Case => O(n)
Worst Case => O(n)
● Number of passes required to sort the using the radix sort is equal to number of digits in the
largest number in the list.
Range Passes
0 to 9 1 pass
0 to 99 2 passes
0 to 999 3 passes
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
Chapter 02 Searching and Sorting VJTech Academy
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
Chapter 02 Searching and Sorting VJTech Academy
❖ Quick Sort:
● Quick sort is the one of the type of sorting algorithm.
● Quick sort is the fastest internal sorting algorithm.
● It is an unstable sorting algorithm.
● Quick sort is an algorithm of the divide and conquer type.
● In quick sort, we select the pivot for partitioning the given elements. We will choose the first
element of the lists as pivot.
● On the basis of pivot value, list is divided into left partition and right partition.
● Time complexity:
Best Case => O(n log n)
Average Case => O(n log n)
Worst Case => O(n^2)
Step 1: Use two index variables i & j with initial values of 1st index position & n-1 index
position respectively.
Step 2: Compare ith index with pivot element till it finds greater number than pivot element.
Increment ith by 1 if ith element is less than pivot element
Step 3: jth element is compared with pivot element till it finds a number less than pivot element
Decrement jth by 1 if jth element is greater than pivot element.
Step 4: After finding elements greater than & less than pivot elements interchange both the
elements.
Step 5: After interchange again increment & decrement current position of i& j, & compare each
element with pivot element.
Step 6: once all elements are compared with pivot element fix the final position of pivot element
in the list.
Step 7: Divide the total array into 2 parts without including fixed position element.
Step 8: Each part then should be sorted with the same procedure as mentioned above till you get
a sorted array.
● Example:
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
Chapter 02 Searching and Sorting VJTech Academy
● Advantages:
○ Efficient average case as compared to any other sorting algorithm.
○ It is faster than other algorithm such as bubbles sort, selection sort and insertion sort.
● Disadvantages:
○ It is complex and massively recursive.
○ Sorting method is not stable.
○ It can not be used for the external sorting.
○ The worst-case complexity of quick sort is O(n^2), which is worse than the other sorting
algorithms.
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
Chapter 02 Searching and Sorting VJTech Academy
❖ Linear Search:
● Linear search is the one of the type of searching algorithm where key element is search
sequentially in the given list.
● Linear search is also called as “sequential search”.
● Search key element is compared with each element in list
● In this method, searching the key element in given list is begin with the first element and end with
the last element.
● Linear search is too slow to be used with large lists due to its O(n) average case performance.
● Efficient method for less no. of elements.
● Example:
0 1 2 3 4
3 21 11 91 19
● In above example, we are searching for element 91. To search 91 the item 91 is compared with
element at A[0] then A[1] and so on.
● Until we find the key value or reach to the end of array.
● When item is found it displays the “Element found” message else displays “Element not found”.
● Limitation: Highly inefficient for large no of data.
● The time complexity of linear search is O(n).
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
Chapter 02 Searching and Sorting VJTech Academy
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
Chapter 02 Searching and Sorting VJTech Academy
❖ Binary Search:
● Binary search can be performed only on sorted array.
● First find the lower index and upper index of an array and calculate mid index with the formula
(lower + upper)/2.
● Compare the mid position element with the search key element.
● If both are equal then stop the search process. If both are not equal then divide list into two parts.
● If the search element is less than mid position element then change the upper index value and use
first half of the list for further searching process.
● If the search element is greater than mid position element than change the lower index value and
use second half of the list for further searching process.
● Again find lower and upper index value and calculate mid value.
● Repeat the process till element is found or lower index value is less than or equal to upper index
value.
● The time complexity of binary search is O(log n).
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
Chapter 02 Searching and Sorting VJTech Academy
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
Chapter 02 Searching and Sorting VJTech Academy
if(i<=j)
printf("\n Element is found!!!");
else
printf("\n Element is not found!!!");
getch();
}
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
Chapter 02 Searching and Sorting VJTech Academy
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
Chapter 02 Searching and Sorting VJTech Academy
❖ Important Questions:
Data Structure by Mr. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18