Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

1 Lab program Data Structures & Algorithms

The document outlines the implementation of various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, Radix Sort, and Heap Sort. Each algorithm is accompanied by C code examples demonstrating how to sort an array of integers and their respective outputs. The document aims to compare the performance of these algorithms in terms of time and space complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

1 Lab program Data Structures & Algorithms

The document outlines the implementation of various sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Quick Sort, Merge Sort, Radix Sort, and Heap Sort. Each algorithm is accompanied by C code examples demonstrating how to sort an array of integers and their respective outputs. The document aims to compare the performance of these algorithms in terms of time and space complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1.

Sorting Algorithms: Implement various sorting algorithms such as


Bubble sort, Selection sort, Insertion sort, Quick sort, Merge sort, Radix
sort, and Heap sort, and compare their performance in terms of time and
space complexity.

Bubble Sort

#include <stdio.h>

int main(){

int arr[50], num, x, y, temp;

printf("Please Enter the Number of Elements you want in the array: ");

scanf("%d", &num);

printf("Please Enter the Value of Elements: ");

for(x = 0; x < num; x++)

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

for(x = 0; x < num - 1; x++){

for(y = 0; y < num - x - 1; y++){

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

temp = arr[y];

arr[y] = arr[y + 1];

arr[y + 1] = temp;

printf("Array after implementing bubble sort: ");

for(x = 0; x < num; x++){


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

return 0;

Output

Please Enter the Number of Elements you want in the array: 10

Please Enter the Value of Elements: 7

10

Array after implementing bubble sort: 1 2 3 4 5 6 7 8 9 10

Selection Sort

#include <stdio.h>

void selection_sort();

int a[30], n;

void main()

int i;
printf("\nEnter size of an array: ");

scanf("%d", &n);

printf("\nEnter elements of an array:\n");

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

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

selection_sort();

printf("\n\nAfter sorting:\n");

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

printf("\n%d", a[i]);

void selection_sort()

int i, j, min, temp;

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

min = i;

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

if (a[j] < a[min])

min = j;

temp = a[i];

a[i] = a[min];

a[min] = temp;
}

OUTPUT:

Enter size of an array: 6

Enter elements of an array:

After sorting:

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 arr[] = { 12, 11, 13, 5, 6 };

int n = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, n);

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

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

printf("\n");

return 0;

Output

5 6 11 12 13
Quick Sort

#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

if(first<last){

pivot=first;

i=first;

j=last;

while(i<j){

while(number[i]<=number[pivot]&&i<last)

i++;

while(number[j]>number[pivot])

j--;

if(i<j){

temp=number[i];

number[i]=number[j];

number[j]=temp;

} }

temp=number[pivot];

number[pivot]=number[j];

number[j]=temp;

quicksort(number,first,j-1);

quicksort(number,j+1,last);

}
}

int main(){

int i, count, number[25];

printf("Enter some elements (Max. - 25): ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

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

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

quicksort(number,0,count-1);

printf("The Sorted Order is: ");

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

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

return 0;

Output

Enter some elements (Max. - 25): 6

Enter 6 elements: 8

-3

The Sorted Order is: -3 1 2 3 6 8


Merge Sort

#include <stdio.h>

#define max 10

int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };

int b[10];

void merging(int low, int mid, int high) {

int l1, l2, i;

for(l1 = low, l2 = mid + 1, i = low; l1 <= mid && l2 <= high; i++) {

if(a[l1] <= a[l2])

b[i] = a[l1++];

else

b[i] = a[l2++];

while(l1 <= mid)

b[i++] = a[l1++];

while(l2 <= high)

b[i++] = a[l2++];

for(i = low; i <= high; i++)

a[i] = b[i];

void sort(int low, int high) {

int mid;

if(low < high) {

mid = (low + high) / 2;


sort(low, mid);

sort(mid+1, high);

merging(low, mid, high);

} else {

return;

int main() {

int i;

printf("List before sorting\n");

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

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

sort(0, max);

printf("\nList after sorting\n");

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

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

Output

List before sorting

10 14 19 26 27 31 33 35 42 44 0

List after sorting

0 10 14 19 26 27 31 33 35 42 44
Radix Sort

#include<stdio.h>

int get_max (int a[], int n){

int max = a[0];

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

if (a[i] > max)

max = a[i];

return max;

void radix_sort (int a[], int n){

int bucket[10][10], bucket_cnt[10];

int i, j, k, r, NOP = 0, divisor = 1, lar, pass;

lar = get_max (a, n);

while (lar > 0){

NOP++;

lar /= 10;

for (pass = 0; pass < NOP; pass++){

for (i = 0; i < 10; i++){

bucket_cnt[i] = 0;

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

r = (a[i] / divisor) % 10;

bucket[r][bucket_cnt[r]] = a[i];
bucket_cnt[r] += 1;

i = 0;

for (k = 0; k < 10; k++){

for (j = 0; j < bucket_cnt[k]; j++){

a[i] = bucket[k][j];

i++;

divisor *= 10;

printf ("After pass %d : ", pass + 1);

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

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

printf (" ");

int main (){

int i, n, a[10];

printf ("Enter the number of items to be sorted: ");

scanf ("%d", &n);

printf ("Enter items: ");

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

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

}
radix_sort (a, n);

printf ("Sorted items : ");

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

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

printf (" ");

return 0;

Output

Enter number of items to be sorted 6


Enter items:567 789 121 212 563 562
After pass 1 : 121 212 562 563 567 789
After pass 2 : 212 121 562 563 567 789
After pass 3 : 121 212 562 563 567 789
Sorted items : 121 212 562 563 567 789

Heap Sort

#include <stdio.h>

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

int tmp = *a;

*a = *b;

*b = tmp;

void heapify(int arr[], int n, int i) {

int max = i; //Initialize max as root

int leftChild = 2 * i + 1;
int rightChild = 2 * i + 2;

//If left child is greater than root

if (leftChild < n && arr[leftChild] > arr[max])

max = leftChild;

//If right child is greater than max

if (rightChild < n && arr[rightChild] > arr[max])

max = rightChild;

//If max is not root

if (max != i) {

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

//heapify the affected sub-tree recursively

heapify(arr, n, max);

//Main function to perform heap sort

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

//Rearrange array (building heap)

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

//Extract elements from heap one by one

for (int i = n - 1; i >= 0; i--) {

swap(&arr[0], &arr[i]); //Current root moved to the end

heapify(arr, i, 0); //calling max heapify on the heap reduced

}
}

//print size of array n using utility function

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

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

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

printf("\n");

//Driver code

int main() {

int arr[] = {11, 34, 9, 5, 16, 10};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array:\n");

display(arr, n);

heapSort(arr, n);

printf("Sorted array:\n");

display(arr, n);

Output

Original array:

11 34 9 5 16 10

Sorted array:

5 9 10 11 16 34

You might also like