Sorting Techniques
Sorting Techniques
Sorting Techniques
By:Manoj
Bubble Sort
• Compares two adjacent elements from left to right, and swaps them until they are
in the intended order.
• Starting with the first element(index = 0), compare the current element with the next
element of the array.
• If the current element is greater than the next element of the array, swap them.
• If the current element is less than the next element, move to the next element.
• Repeat Step 1.
Program
#include<iostream> {
using namespace std; cout<<arr[i]<<" ";
void bubbleSort(int arr[], int size) { }
for (int i = 0; i < size - 1; ++i) { }
for (int j = 0; j < size - i - 1; ++j) { int main()
if (arr[j] > arr[j + 1]) {
{ int arr[5]={5,3,7,2,9};
int temp = arr[j]; bubbleSort(arr,5);
arr[j] = arr[j+1]; }
arr[j+1] = temp;
}
}
}
cout<<"Sorted Array: ";
for(int i = 0; i < size; i++)
Selection Sort
Selection Sort
• Selects the smallest element from an unsorted list in each iteration and places that element
to its appropriate position into the array.
int minIndex = i;
// Find the index of the minimum element in the unsorted part of the array
minIndex = j;
// Swap the minimum element with the current element without using std::swap
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
int main() {
selectionSort(arr, n);
return 0; }
Insertion Sort
Algorithm
Insertion Sort Algorithm
• The elements from the unsorted part of the array are inserted at the correct position in the
sorted part.
• Hence, the name is insertion sort, since elements are inserted at the correct position.
• It is assumed that the first card is already sorted in the card game, and then we select an
unsorted card.
• If the selected unsorted card is greater than the first card, it will be placed at the right side;
otherwise, it will be placed at the left side.
• Similarly, all unsorted cards are taken and put in their exact place.
#include <iostream>
int j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = key;
} }
int main() { for (int i = 0; i < n; i++) {
int arr[] = {12, 11, 13, 5, 6}; cout << arr[i] << " ";
insertionSort(arr, n);
• In the divide and conquer algorithm, the problem is divided into multiple sub problems.
• Each sub problem is then solved, and then combine all the solutions to these sub problems
to get the solution of the whole problem.
This algorithm has three steps
3. Combine: combine all the solutions of all the subproblems to get the solution to the
whole problem.
The specific computer algorithms are based on the Divide & Conquer approach
• Binary Search
• Tower of Hanoi.
Merge Sort
• If n == 1, ie. there is only one element in the array, then the array is already sorted
• If n > 1, then initialize variables left and right as left = 0 and right = n-1.
• Call mergeSort(arr, left, middle) to recursively sort the first half of the array.
• Similarly, call mergeSort(arr, middle+1, right) to recursively sort the other half of the array.
• Finally call merge(arr, left, middle, right) to merge the obtained sorted arrays.