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

Sorting

sorting

Uploaded by

Subhajit Mandal
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sorting

sorting

Uploaded by

Subhajit Mandal
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Merge sort is defined as a sorting algorithm that works by dividing an array into

smaller subarrays, sorting each subarray, and then merging the sorted subarrays
back together to form the final sorted array.

Lets consider an array arr[] = {38, 27, 43, 10}

1.Initially divide the array into two equal halves:

2.These subarrays are further divided into two halves. Now they become array of
unit length that can no longer be divided and array of unit length are always
sorted.
3.mergeing the unit length of subarrays into sorted subarrays.
4.These sorted subarrays are merged together, and we get bigger sorted subarrays.

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

// Create temp arrays


int L[n1], R[n2];

// Copy data to temp arrays L[] and R[]


for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];

// Merge the temp arrays back into arr[l..r


i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

// Copy the remaining elements of R[],


// if there are any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// l is for left index and r is right index of the
// sub-array of arr to be sorted
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;

// Sort first and second halves


mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);

merge(arr, l, m, r);
}
}

QuickSort is a sorting algorithm that picks an element as a pivot and partitions


the given array around the picked pivot by placing the pivot in its correct
position in the sorted array.

int partition(int arr[], int low, int high)


{

// initialize pivot to be the first element


int pivot = arr[low];
int i = low;
int j = high;

while (i < j) {

// condition 1: find the first element greater than


// the pivot (from starting)
while (arr[i] <= pivot && i <= high - 1) {
i++;
}

// condition 2: find the first element smaller than


// the pivot (from last)
while (arr[j] > pivot && j >= low + 1) {
j--;
}
if (i < j) {
swap(&arr[i], &arr[j]);
}
}
swap(&arr[low], &arr[j]);
return j;
}
void quickSort(int arr[], int low, int high)
{
if (low < high) {

// call Partition function to find Partition Index


int partitionIndex = partition(arr, low, high);

// Recursively call quickSort() for left and right


// half based on partition Index
quickSort(arr, low, partitionIndex - 1);
quickSort(arr, partitionIndex + 1, high);
}
}

Bubble sort is a simple sorting algorithm that works by comparing the adjacent
elements in the list and swapping them if the elements are not in the specified
order.

Run two loops nested in one another.

The outer loop will run from i = 0 to i < n – 1, where n is the number of elements
in the list.

The inner loop will run from j = 0 to j < n – i – 1. It is because, after each
iteration of the outer loop, one element at the end (or at the start if the order
is decreasing order) will be in its right place so we can leave it as it is.

In the inner loop, we will check if the arr[ j ] > arr[ j + 1 ].


If it’s true, then we will swap places of these elements.
If false, we will continue to the next iteration.

This process will be repeated till the conditions of the loop are satisfied.

void swap(int* arr, int i, int j)


{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

// A function to implement bubble sort


void bubbleSort(int arr[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)

// Last i elements are already


// in place
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr, j, j + 1);
}

Selection sort is a simple and efficient sorting algorithm that works by repeatedly
selecting the smallest (or largest) element from the unsorted portion of the list
and moving it to the sorted portion of the list.

void selectionSort(int arr[], int n)


{
int i, j, min_idx;

// One by one move boundary of unsorted subarray


for (i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
// Swap the found minimum element with the first element
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}

Insertion sort is an algorithm used to sort a collection of elements in ascending


or descending order. The basic idea behind the algorithm is to divide the list into
two parts: a sorted part and an unsorted part.

void insertionSort(int arr[], int n)


{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

/* Move elements of arr[0..i-1],


that are greater than key,
to one position ahead of
their current position */
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

Heap sort is a comparison-based sorting technique based on Binary Heap data


structure. It is similar to the selection sort where we first find the minimum
element and place the minimum element at the beginning. Repeat the same process for
the remaining elements.

You might also like