Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Sorting Algorithms Implementation

The document contains source code and analysis of insertion sort, selection sort, bubble sort, merge sort, recursive insertion sort, heap sort, and quick sort algorithms. It includes the pseudocode implementation of each sorting algorithm. For each algorithm, it analyzes the time complexity in best, average and worst cases and space complexity. The time complexity analysis shows insertion, selection and bubble sorts have O(n^2) time complexity while merge sort, heap sort and quick sort have O(nlogn) time complexity. All the algorithms have O(1) auxiliary space complexity except recursive insertion sort which uses O(n) space. </SUMMARY>

Uploaded by

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

Sorting Algorithms Implementation

The document contains source code and analysis of insertion sort, selection sort, bubble sort, merge sort, recursive insertion sort, heap sort, and quick sort algorithms. It includes the pseudocode implementation of each sorting algorithm. For each algorithm, it analyzes the time complexity in best, average and worst cases and space complexity. The time complexity analysis shows insertion, selection and bubble sorts have O(n^2) time complexity while merge sort, heap sort and quick sort have O(nlogn) time complexity. All the algorithms have O(1) auxiliary space complexity except recursive insertion sort which uses O(n) space. </SUMMARY>

Uploaded by

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

JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

EXPERIMENT-1.1

AIM:- TO IMPLEMENT INSERTION SORT

SOURCE CODE:-

#include<stdio.h>
void insertionsort(int arr[],int len)
{ int temp=0;int j;
for(int i=1;i<len;i++)
{
temp=ar
r[i]; j=i-1;
while(j>=0&&arr[j]>temp)
{
arr[j+1]=ar
r[j]; j--;
}
arr[j+1]=temp;
}
}

int main()
{
printf("ENTER THE LENGTH OF THE ARRAY\n");
int len;
scanf("%d",&l
en); int arr[100];
for(int i=0;i<len;i+
+)
{
printf("ENTER %d element in the array :
\n",i+1); scanf("%d",&arr[i]);
}
insertionsort(arr,len);
for(int i=0;i<len;i++)
{
printf("%d ",arr[i]);
}
return 0;
}

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

OUTPUT:-

TIME COMPLEXITY:- BEST CASE-O(N) AVERAGE CASE-O(N^2) WORST CASE-O(N^2) SPACE COMPLEXITY-O(1)

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

EXPERIMENT-1.2

AIM:- TO IMPLEMENT SELECTION SORT

SOURCE CODE:-

#include <stdio.h> void


swap(int *a, int *b) { int
temp = *a; *a = *b;
*b = temp;
}
void selectionSort(int array[], int size)
{ for (int step = 0; step < size - 1; step++)
{ int min_idx = step; for (int i = step +
1; i < size; i++) { if (array[i] <
array[min_idx])
{
min_idx = i;
}
}
swap(&array[min_idx], &array[step]);
}
}

void printArray(int array[], int size)


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

int main() {
int data[] = {20, 12, 10, 15, 2}; int size =
sizeof(data) / sizeof(data[0]);
selectionSort(data, size); printf("Sorted array
in Acsending Order:\n");
printArray(data, size);
}

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

OUTPUT:-

TIME COMPLEXITY:-

BEST CASE-O(N^2)

AVERAGE CASE-O(N^2)

WORST CASE-O(N^2)

SPACE COMPLEXITY-O(1)

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

EXPERIMENT-1.3

AIM:- TO IMPLEMENT BUBBLE SORT

SOURCE CODE:-
#include <stdio.h>
void bubbleSort(int array[], int size) {

for (int step = 0; step < size - 1; ++step) {


for (int i = 0; i < size - step - 1; ++i)
{
if (array[i] > array[i + 1]) {

// swapping occurs if elements


// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}

void printArray(int array[], int size)


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

int main() {
int data[] = {-2, 45, 0, 11, -9};

// find the array's length


int size = sizeof(data) / sizeof(data[0]);

bubbleSort(data, size);

printf("Sorted Array in Ascending Order:\n");


printArray(data, size);

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

OUTPUT:-

TIME COMPLEXITY:- BEST CASE-O(N) AVERAGE CASE-O(N^2) WORST CASE-O(N^2) SPACE COMPLEXITY-O(1)

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

EXPERIMENT-2.1

AIM:- TO IMPLEMENT MERGE SORT

SOURCE CODE:- #include


<stdio.h>
void merge(int arr[], int p, int q, int r)
{ int n1 = q - p + 1;
int n2 = r - q;

int L[n1], M[n2];

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


L[i] = arr[p + i]; for (int j
= 0; j < n2; j++) M[j] =
arr[q + 1 + j]; int i, j, k;
i = 0; j = 0; k = p;
while (i < n1 && j < n2) {
if (L[i] <= M[j]) { arr[k]
= L[i]; i++; } else {
arr[k] = M[j]; j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


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

void mergeSort(int arr[], int l, int r)


{ if (l < r) {
int m = l + (r - l) / 2;

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++)
printf("%d ", arr[i]); printf("\n");
}

int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, size - 1);

printf("Sorted array: \n");


printArray(arr, size);
}

OUTPUT:-

TIME COMPLEXITY:- BEST


CASE:- O(nlogn)
AVERAGE CASE:- O(nlogn) WORST CASE:- O(nlogn)

SPACE COMPLEXITY:- O(1)

EXPERIMENT-2.2

AIM:- TO IMPLEMENT RECURSIVE INSERTION SORT

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

SOURCE CODE:-

#include <stdio.h>
void insertionSortRecursive(int arr[], int n)
{
if (n <= 1)
{
return;
}

insertionSortRecursive(arr, n - 1);
int last = arr[n - 1]; int j = n - 2;
while (j >= 0 && arr[j] > last) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = last;
}

void printArray(int arr[], int size)


{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);

insertionSortRecursive(arr, n);
printArray(arr, n);

return 0;
}

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

OUTPUT:-

TIME COMPLEXITY:- O(n2 )

SPACE COMPLEXITY:- O(n)

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

EXPERIMENT-2.3

AIM:- TO IMPLEMENT HEAP SORT

SOURCE CODE:-

#include <stdio.h>
void swap(int* a, int* b)
{

int temp = *a;


*a = *b;
*b = temp;
}

void heapify(int arr[], int N, int i)


{

int largest = i;

int left = 2 * i + 1;

int right = 2 * i + 2;

if (left < N && arr[left] > arr[largest])

largest = left;

if (right < N && arr[right] > arr[largest])

largest = right;

if (largest != i) {

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

heapify(arr, N, largest);
}
}

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

void heapSort(int arr[], int N)


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

heapify(arr, N, i);
for (int i = N - 1; i >= 0; i--) {

swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int N)
{
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);

heapSort(arr, N);
printf("Sorted array is\n"); printArray(arr, N);
}

OUTPUT:-

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

TIME COMPLEXITY:- BEST


CASE:- O(nlogn)
AVERAGE CASE:- O(nlogn) WORST CASE:- O(nlogn)

SPACE COMPLEXITY:- O(1)

EXPERIMENT-2.4

AIM:- TO IMPLEMENT QUICK SORT

SOURCE CODE:-
#include <stdio.h>
void swap(int* a, int* b)
{
int t = *a;

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

*a = *b;
*b = t;
}
int partition(int arr[], int low, int high)
{

int pivot = arr[high];

int i = (low - 1);

for (int j = low; j <= high - 1; 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);
}
}

int main()
{
int arr[] = { 10, 7, 8, 9, 1, 5 };
int N = sizeof(arr) / sizeof(arr[0]);

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131
JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

quickSort(arr, 0, N - 1);
printf("Sorted array: \n"); for
(int i = 0; i < N; i++)
printf("%d ", arr[i]);
return 0;
}

OUTPUT:-

TIME COMPLEXITY:-

BEST CASE:- O(nlogn) AVERAGE CASE:- O(nlogn) WORST CASE:- O(n2)

SPACE COMPLEXITY:- O(n)

DESIGN AND ANALYSIS OF ALGORITHM


(KCS 553) PRASHANT YADAV 2100910100131

You might also like