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

sorting PROGRAMS

The document provides implementations of four sorting algorithms in C: Bubble Sort, Insertion Sort, Selection Sort, and Quicksort. Each algorithm includes a function for sorting an array and a main function to input the array size and elements, followed by printing the sorted array. The code examples demonstrate basic sorting techniques commonly used in programming.

Uploaded by

thanoojpamanji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

sorting PROGRAMS

The document provides implementations of four sorting algorithms in C: Bubble Sort, Insertion Sort, Selection Sort, and Quicksort. Each algorithm includes a function for sorting an array and a main function to input the array size and elements, followed by printing the sorted array. The code examples demonstrate basic sorting techniques commonly used in programming.

Uploaded by

thanoojpamanji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

SORTING : ​

// 1. Bubble Sort
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
int main() {
int arr[100], n;
printf("Enter size: ");
scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

c
CopyEdit
// 2. Insertion Sort
#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i], j = i - 1;
while (j >= 0 && arr[j] > key) arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
}
int main() {
int arr[100], n;
printf("Enter size: ");
scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

c
CopyEdit
// 3. Selection Sort
#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min]) min = j;
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
int main() {
int arr[100], n;
printf("Enter size: ");
scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

c
CopyEdit
// 4. Quicksort
#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], i = low - 1;
for (int j = low; j < high; j++)
if (arr[j] < pivot) swap(&arr[++i], &arr[j]);
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int arr[100], n;
printf("Enter size: ");
scanf("%d", &n);
printf("Enter elements: ");
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

You might also like