Sorting Techniques

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 36

Sorting Techniques

By:Manoj
Bubble Sort

• Compares two adjacent elements from left to right, and swaps them until they are
in the intended order.

• Worst-case complexity of Bubble sort is O(n2).


Implementing Bubble Sort Algorithm

• 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.

• worst-case complexity of selection sort is O(n2)


Program

void selectionSort(int arr[], int n) {

for (int i = 0; i < n - 1; ++i) {

int minIndex = i;

// Find the index of the minimum element in the unsorted part of the array

for (int j = i + 1; j < n; ++j) {

if (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the minimum element with the current element without using std::swap

int temp = arr[i];

arr[i] = arr[minIndex];

arr[minIndex] = temp;

}
int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);

cout << "Original array: ";

for (int i = 0; i < n; ++i) {

cout << arr[i] << " ";

cout << endl;

selectionSort(arr, n);

cout << "Sorted array: ";

for (int i = 0; i < n; ++i) {

cout << arr[i] << " ";

cout << endl;

return 0; }
Insertion Sort
Algorithm
Insertion Sort Algorithm

• The array is split into sorted and unsorted parts.

• 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.

• worst case is O(n2)


• Insertion sort works similar to the sorting of playing cards in hands.

• 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>

using namespace std;

void insertionSort(int arr[], int n) {

for (int i = 1; i < n; i++) {

int key = arr[i];

int j = i - 1;

while (j >= 0 && arr[j] > key) {

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] << " ";

int n = sizeof(arr) / sizeof(arr[0]); }

cout << "Original Array: "; cout << endl;

for (int i = 0; i < n; i++) { return 0;

cout << arr[i] << " "; }

cout << endl;

insertionSort(arr, n);

cout << "Sorted Array: ";


Divide and Conquer
Divide and Conquer

• 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

1. Divide: divide the larger problem into multiple subproblems.

2. Conquer: recursively solve every subproblem individually.

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

• Maximum and Minimum Problem

• Binary Search

• Sorting (merge sort, quick sort)

• Tower of Hanoi.
Merge Sort

• Is based on the principle of Divide and Conquer Algorithm.

• Problem is divided into multiple sub-problems.

• Each sub-problem is solved individually.

• Finally, sub-problems are combined to form the final solution.


Algorithm

• We are given an array arr of size n.

• Check, if n == 0, i.e., we have an empty array, then we have no element to 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.

• Find middle = (left + right)/2.

• 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy