0% found this document useful (0 votes)
42 views15 pages

Assignment 7

The document contains five different sorting algorithms implemented in C: Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick 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 sorted array. The output examples demonstrate the functionality of each sorting algorithm with sample input and output.

Uploaded by

Vibhuti Sachdeva
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)
42 views15 pages

Assignment 7

The document contains five different sorting algorithms implemented in C: Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick 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 sorted array. The output examples demonstrate the functionality of each sorting algorithm with sample input and output.

Uploaded by

Vibhuti Sachdeva
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

ASSIGNMENT-7

1.CODE:
#include <stdio.h>

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


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

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


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

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


scanf("%d", &n);

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

bubbleSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
OUTPUT:
Enter the number of elements: 4
Enter the elements:
12 45 67 85
Sorted array: 12 45 67 85

--------------------------------
Process exited after 8.19 seconds with return value 0
Press any key to continue . . .

2.CODE:
#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;
}
}
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}

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


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

int main() {
int n, i;

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


scanf("%d", &n);

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

selectionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter the elements:
34 45 76 54 24
Sorted array: 24 34 45 54 76

--------------------------------
Process exited after 13.19 seconds with return value 0
Press any key to continue . . .

3.CODE:
#include <stdio.h>
void insertionSort(int arr[], int n) {
int i, j, key;
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;
}
}

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


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

int main() {
int n, i;

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


scanf("%d", &n);

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

insertionSort(arr, n);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter the elements:
14635
Sorted array: 1 3 4 5 6
--------------------------------
Process exited after 10.53 seconds with return value 0
Press any key to continue . . .
4.CODE:

#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;

int L[n1], R[n2];

for (i = 0; i < n1; i++)


L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];

i = 0;
j = 0;
k = left;

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++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

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


if (left < right) {
int mid = left + (right - left) / 2;

mergeSort(arr, left, mid);


mergeSort(arr, mid + 1, right);

merge(arr, left, mid, right);


}
}

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


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

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


scanf("%d", &n);

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

mergeSort(arr, 0, n - 1);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
OUTPUT:
Enter the number of elements: 5
Enter the elements:
23 45 2 56 76 21
Sorted array: 2 23 45 56 76

--------------------------------
Process exited after 37.17 seconds with return value 0
Press any key to continue . . .

5.CODE:
#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];
int i = (low - 1);

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


if (arr[j] < pivot) {
i++;
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);
}
}

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


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

int main() {
int n, i;

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


scanf("%d", &n);

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

quickSort(arr, 0, n - 1);

printf("Sorted array: ");


printArray(arr, n);

return 0;
}
OUTPUT:
Enter the number of elements: 4
Enter the elements:
1 45 76 23
Sorted array: 1 23 45 76
--------------------------------
Process exited after 10.55 seconds with return value 0
Press any key to continue . . .

You might also like