Sorting is a method to arrange a set of elements in either increasing or decreasing order according to some basis/relationship among the elements.
#include
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
void bubbleSort(int *A, int n)
{
int temp;
int isSorted = 0;
for (int i = 0; i < n - 1; i++) // For number of pass
{
printf("Working on pass number %d\n", i + 1);
for (int j = 0; j < n - 1 - i; j++) // For comparison in each pass
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
void bubbleSortAdaptive(int *A, int n)
{
int temp;
int isSorted = 0;
for (int i = 0; i < n - 1; i++) // For number of pass
{
printf("Working on pass number %d\n", i + 1);
isSorted = 1;
for (int j = 0; j < n - 1 - i; j++) // For comparison in each pass
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
isSorted = 0;
}
}
if (isSorted)
{
return;
}
}
}
int main()
{
// int A[] = {12, 54, 65, 7, 23, 9};
int A[] = {1, 2, 5, 6, 12, 54, 625, 7, 23, 9, 987};
// int A[] = {1, 2, 3, 4, 5, 6};
int n = 11;
printArray(A, n); // Printing the array before sorting
bubbleSort(A, n); // Function to sort the array
printArray(A, n); // Printing the array before sorting
return 0;
}
Conclusively, we had to have 4 passes to sort an array of length 5. And in the first pass, we had to compare the to-be inserted element with just one single element 7. So, only one comparison, and one possible swap. Similarly, for ith pass, we would have i number of comparisons, and i possible swaps.
#include <stdio.h>
void printArr(int arr[], int n)
{
printf("array -> ");
for (int i = 0; i < n; i++)
{
printf(" %d ", arr[i]);
}
printf("\n");
}
void insertionSort(int *arr, int n)
{
int current;
int j;
for (int i = 1; i < n; i++)
{
current = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > current)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = current;
}
}
int main()
{
int A[] = {12, 54, 65, 7, 23, 9};
int n = 6;
printArr(A, n);
insertionSort(A, n);
printArr(A, n);
return 0;
<
In each pass till 5 it gets sorted
And this is why the Selection Sort algorithm got its name. We select the minimum element at each pass and give it its final position. Few conclusions before we proceed to the programming segment:
void selectionSort(int *A, int n){
int indexOfMin, temp;
printf("Running Selection sort...\n");
for (int i = 0; i < n-1; i++)
{
indexOfMin = i;
for (int j = i+1; j < n; j++)
{
if(A[j] < A[indexOfMin]){
indexOfMin = j;
}
}
// Swap A[i] and A[indexOfMin]
temp = A[i];
A[i] = A[indexOfMin];
A[indexOfMin] = temp;
}
}
Unsorted array ↓
#include
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int partition(int A[], int low, int high)
{
int pivot = A[low];
int i = low + 1;
int j = high;
int temp;
do
{
while (A[i] <= pivot)
{
i++;
}
while (A[j] > pivot)
{
j--;
}
if (i < j)
{
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
} while (i < j);
// Swap A[low] and A[j]
temp = A[low];
A[low] = A[j];
A[j] = temp;
return j;
}
void quickSort(int A[], int low, int high)
{
int partitionIndex; // Index of pivot after partition
if (low < high)
{
partitionIndex = partition(A, low, high);
quickSort(A, low, partitionIndex - 1); // sort left subarray
quickSort(A, partitionIndex + 1, high); // sort right subarray
}
}
int main()
{
// int A[] = {3, 5, 2, 13, 12, 3, 2, 13, 45};
int A[] = {9, 4, 4, 8, 7, 5, 6};
// 3, 5, 2, 13, 12, 3, 2, 13, 45
// 3, 2, 2, 13i, 12, 3j, 5, 13, 45
// 3, 2, 2, 3j, 12i, 13, 5, 13, 45 --> first call to partition returns 3
int n = 9;
n = 7;
printArray(A, n);
quickSort(A, 0, n - 1);
printArray(A, n);
return 0;
}
For merging two sorted subarrays of a single array in the array itself we modify the code
#include <stdio.h>
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
void merge(int A[], int mid, int low, int high)
{
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;
while (i <= mid && j <= high)
{
if (A[i] < A[j])
{
B[k] = A[i];
i++;
k++;
}
else
{
B[k] = A[j];
j++;
k++;
}
}
while (i <= mid)
{
B[k] = A[i];
k++;
i++;
}
while (j <= high)
{
B[k] = A[j];
k++;
j++;
}
for (int i = low; i <= high; i++)
{
A[i] = B[i];
}
}
void mergeSort(int A[], int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergeSort(A, low, mid);
mergeSort(A, mid + 1, high);
merge(A, mid, low, high);
}
}
int main()
{
// int A[] = {9, 14, 4, 8, 7, 5, 6};
int A[] = {9, 1, 4, 14, 4, 15, 6};
int n = 7;
printArray(A, n);
mergeSort(A, 0, 6);
printArray(A, n);
return 0;
}
Array we will sort ↓
#include<stdio.h>
#include<limits.h>
#include<stdlib.h>
void printArray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d ", A[i]);
}
printf("\n");
}
int maximum(int A[], int n){
int max = INT_MIN;
for (int i = 0; i < n; i++)
{
if (max < A[i]){
max = A[i];
}
}
return max;
}
void countSort(int * A, int n){
int i, j;
// Find the maximum element in A
int max = maximum(A, n);
// Create the count array
int* count = (int *) malloc((max+1)*sizeof(int));
// Initialize the array elements to 0
for (i = 0; i < max+1; i++)
{
count[i] = 0;
}
// Increment the corresponding index in the count array
for (i = 0; i < n; i++)
{
count[A[i]] = count[A[i]] + 1;
}
i =0; // counter for count array
j =0; // counter for given array A
while(i<= max){
if(count[i]>0){
A[j] = i;
count[i] = count[i] - 1;
j++;
}
else{
i++;
}
}
}
int main(){
int A[] = {9, 1, 4, 14, 4, 15, 6};
int n = 7;
printArray(A, n);
countSort(A, n);
printArray(A, n);
return 0;
}