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

sorting C programs

The document contains implementations of five sorting algorithms in C: Insertion Sort, Bubble Sort, Selection Sort, Quick Sort, and Merge Sort. Each algorithm includes a main function that prompts the user for the number of elements and the elements themselves, sorts the array, and then prints the original and sorted arrays. The code demonstrates the basic structure and logic for each sorting method.

Uploaded by

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

sorting C programs

The document contains implementations of five sorting algorithms in C: Insertion Sort, Bubble Sort, Selection Sort, Quick Sort, and Merge Sort. Each algorithm includes a main function that prompts the user for the number of elements and the elements themselves, sorts the array, and then prints the original and sorted arrays. The code demonstrates the basic structure and logic for each sorting method.

Uploaded by

Arpit Rawat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Insertion Sort

#include <stdio.h>

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

int i, key, j;

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

key = arr[i];

j = i - 1;

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

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

int main() {

int n;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);
}

printf("Original array: \n");

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

printf("%d ", arr[i]);

insertionSort(arr, n);

printf("\nSorted array: \n");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

Bubble sort

#include <stdio.h>

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

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

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

// Swap if the element found is greater than the next element

if (arr[j] > arr[j + 1]) {

temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int main() {

int n;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

}
printf("Original array: \n");

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

printf("%d ", arr[i]);

bubbleSort(arr, n);

printf("\nSorted array (using Bubble Sort): \n");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

Selection Sort

#include <stdio.h>
void selectionSort(int arr[], int n) {

int i, j, minIndex, temp;

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

minIndex = i;

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

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

minIndex = j;

// Swap the found minimum element with the first element

temp = arr[minIndex];

arr[minIndex] = arr[i];

arr[i] = temp;

int main() {

int n;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

printf("Original array: \n");

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

printf("%d ", arr[i]);

selectionSort(arr, n);

printf("\nSorted array (using Selection Sort): \n");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

}
Quick Sort
#include <stdio.h>

void swap(int* a, int* b) {

int temp = *a;

*a = *b;

*b = temp;

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

int pivot = arr[high]; // Choose the rightmost element as the pivot

int i = (low - 1); // Initialize the index of the smaller element

for (int j = low; j <= high - 1; j++) {

// If the current element is smaller than or equal to the pivot

if (arr[j] <= pivot) {

i++; // Increment index of the smaller element

swap(&arr[i], &arr[j]);

}
// Swap the pivot element with the element at the (i + 1)th
position

swap(&arr[i + 1], &arr[high]);

return (i + 1);

void quickSort(int arr[], int low, int high) {

if (low < high) {

// Partition the array into two subarrays

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

// Recursively sort the subarrays

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

int main() {

int n;

printf("Enter the number of elements in the array: ");

scanf("%d", &n);
int arr[n];

printf("Enter %d elements:\n", n);

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

scanf("%d", &arr[i]);

printf("Original array: \n");

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

printf("%d ", arr[i]);

quickSort(arr, 0, n - 1);

printf("\nSorted array (using Quick Sort): \n");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

}
Merge Sort
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
// Create temporary arrays
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

// Merge the temporary arrays back into arr[left..right]


i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = left; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
// Copy the remaining elements of L[], if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[], if any
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
// Same as (left+right)/2, but avoids overflow for large
left and right
int mid = left + (right - left) / 2;
// Sort first and second halves
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
// Merge the sorted halves
merge(arr, left, mid, right);
}
}

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Original array: \n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

mergeSort(arr, 0, n - 1);

printf("\nSorted array (using Merge Sort): \n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

You might also like