Sorting Algorithms
Sorting Algorithms
Sorting Algorithms
Introduction
• Sorting and searching are two of the most common operations performed by computers all around
the world.
• The sorting operation arranges the numerical and alphabetical data present in a list, in a specific
order or sequence.
• Searching, on the other hand, locates a specific element across a given list of elements.
• At times, a list may require sorting before the search operation can be performed on it.
Sorting Techniques
• Internal Sorting
• External Sorting
Internal Sorting
• All sorting techniques which require the data set to be present in the main memory are referred as
internal sorting techniques.
Examples
• Selection sort
• Bubble sort
• Insertion sort
• Shell sort
• Quick sort
• Heap sort
External Sorting
• Since the number of objects to be sorted is too large to fit in main memory.
Examples
• Merge Sort
• Multiway Merge
• Polyphase merge
Bubble Sort
• Bubble sort is one of the simplest internal sorting algorithms. Bubble sort works by comparing two
consecutive elements and the largest element among these two bubbles towards right.
• At the end of the first pass the largest element gets sorted and placed at the end of the sorted list.
• This process is repeated for all pairs of the elements until it moves the largest element to the end
of the list in that iteration.
• Bubble sort consists of n-1 passes, where ‘n’ is the number of elements to be sorted.
• In the 1st pass, the largest element will be placed in the nth position.
• In the 2nd pass, the second largest element will be placed in the (n-1)th position.
• Assume that an array containing n elements is sorted using bubble sort technique.
= n * (n – 1) / 2
= O(n2)
• It leverages the presence of any existing sort pattern in the list, thus resulting in better
efficiency.
Limitations of Bubble Sort
• The efficiency of O(n2) is not well suited for large sized lists.
• It is slow in execution as large elements are moved towards the end of the list in a step-by-step
fashion.
Selection Sort
• It works on the principle of identifying the smallest element in the list and moving it to the
beginning of the list.
• This process is repeated until all the elements in the list are sorted.
Example
• Let us consider an example where a list L contains five integers stored in a random fashion, as
shown:
• Now, if the list L is sorted using selection sort technique then first of all the first element in the
list, i.e., 18 will be selected and compared with all the remaining elements in the list.
• The element which is found to be the lowest amongst the remaining set of elements will be
swapped with the first element.
• Then, the second element will be selected and compared with the remaining elements in the list.
• This process is repeated until all the elements are rearranged in a sorted manner.
• A single iteration of the selection sorting technique that brings the smallest element at the
beginning of the list is called a pass.
• As we can see in the below, four passes were required to sort a list of five elements.
• Hence, we can say that selection sort requires n-1 passes to sort an array of n elements.
Efficiency of Selection Sort
• Assume that an array containing n elements is sorted using selection sort technique.
• = (n – 1) + (n – 2) + . . . + 1
• = n * (n – 1) / 2
• = O(n2)
• The efficiency of O(n2) is not well suited for large sized lists.
• It does not leverage the presence of any existing sort pattern in the list.
Contains Duplicate
• Given an integer array nums, print true if any value appears at least twice in the array, and print
false if every element is distinct.
• Output: true
• Output: false
#include<stdio.h>
#include<math.h>
int main()
int n,f=0;
scanf("%d", &n);
int a[n];
scanf("%d", &a[i]);
min = i;
min = j;
temp = a[i];
a[i] = a[min];
a[min] = temp;
f=1;
if(f)
printf("false");
else
printf("true");
}
Missing Number
• Given an array nums containing n distinct numbers in the range [0, n], return the only number in
the range that is missing from the array.
• Output: 2
• Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing
for(int i =0;i<numsSize;i++)
arr[nums[i]] = nums[i];
for(int i =0;i<n;i++)
if(arr[i]!=i)
ans = i;
• Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it.
• That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] <
nums[i].
• Output: [4,0,1,1,3]
• Explanation:
• For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).
• For nums[1]=1 does not exist any smaller number than it.
• For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).
int i = 0;
sums[nums[i] + 1]++;
for(i = 1 ; i < 102 ; i++)
sums[i] += sums[i-1];
nums[i] = sums[nums[i]];