Csb 252 assignment
Gaurav singh(211210024)
//bubble sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bubble_Sort(int arr[], int n); //prototype
void bubble_sort(int arr[], int n) //function definition
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;
printf("\n");
printf("Pass%d: \n", i + 1);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
printf("\nSorted array: \n");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\nMemory occupied by input array: %d bytes\n", n*sizeof(int));
printf("Memory occupied by variable i: %d bytes\n", 1*sizeof(int));
printf("Memory occupied by variable j: %d bytes\n", 1*sizeof(int));
printf("Memory occupied by variable temp: %d bytes\n", 1*sizeof(int));
int main(){
clock_t start_t, end_t;
double total_t;
start_t = clock();
printf("Bubble Sort Program in C\n");
int size = 0;
printf("Please enter the number of elements u want to sort: \n");
scanf("%d", &size);
int *arr = (int*)malloc(size * sizeof(int)); //dynamic memory allocation
printf("\nPlease enter the %d numbers: \n", size);
for(int i = 0; i < size; i++){
printf("array[%d]=",i);
scanf("%d", &arr[i]);
fflush(stdin);
bubble_sort(arr, size); //function call
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("\nTotal time taken by CPU: %f\n", total_t);
printf("\nEXITING!\n");
return 0;
}
Selection sort
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void selection_Sort(int arr[], int n); //function prototype
void selection_sort(int arr[], int n) //function definition
for(int i = 0; i < n; i++)
int min = arr[i];
int min_index = 0;
for(int j = i; j < n; j++)
if(min > arr[j]){
min = arr[j];
min_index = j;
int temp = arr[i];
arr[i] = min;
arr[min_index] = temp;
printf("\n");
printf("Pass%d: \n", i + 1);
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\nSorted array: \n");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
printf("\nMemory occupied by input array: %d bytes\n", n*sizeof(int));
printf("Memory occupied by variable i: %d bytes\n", 1*sizeof(int));
printf("Memory occupied by variable j: %d bytes\n", 1*sizeof(int));
printf("Memory occupied by variable temp: %d bytes\n", 1*sizeof(int));
printf("Total Memory occupied: %d bytes\n", n*sizeof(int) + 1*sizeof(int) + 1*sizeof(int) +
1*sizeof(int));
int main(){
clock_t start_t, end_t;
double total_t;
start_t = clock();
printf("Selection Sort Program in C\n");
int size = 0;
printf("\nPlease enter the number of elements u want to sort: \n");
scanf("%d", &size);
int *arr = (int*)malloc(size * sizeof(int)); //dynamic memory allocation
printf("\nPlease enter the %d numbers: \n", size);
for(int i = 0; i < size; i++){
scanf("%d", &arr[i]);
fflush(stdin);
selection_sort(arr, size); //function call
end_t = clock();
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("\nTotal time taken by CPU: %f\n", total_t);
printf("\nEXITING!\n");
return 0;