0% found this document useful (0 votes)
2 views16 pages

DS Sorting Algorithm

This project conducts a comparative study of sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort, analyzing their performance based on time and space complexity. Implemented in C, the study finds that advanced algorithms like Merge Sort and Quick Sort are more efficient for large datasets, while simpler algorithms perform better with smaller datasets. The results highlight the importance of selecting the appropriate sorting algorithm based on data size and application needs.

Uploaded by

ashirbadpatra21
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)
2 views16 pages

DS Sorting Algorithm

This project conducts a comparative study of sorting algorithms including Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, and Quick Sort, analyzing their performance based on time and space complexity. Implemented in C, the study finds that advanced algorithms like Merge Sort and Quick Sort are more efficient for large datasets, while simpler algorithms perform better with smaller datasets. The results highlight the importance of selecting the appropriate sorting algorithm based on data size and application needs.

Uploaded by

ashirbadpatra21
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

D.

S PROJECT : SORTING
ALGORITHMS
PROJECT-1:
TITLE: COMPARATIVE STUDY ON SORTING ALGORITHMS DESCRIPTION

INTRODUCTION:
SECTION : 22
BATCH NO: 14
MEMBERS : 2500032457-ASHIRBAD PATRA
2500032571-NALLAGORLA PRANADEEP
2500032611-L HIMA TEJ
2500032635-KUNDETI KRISHNA KARTHIK

ABSTRACT:

This project presents a comparative study of different sorting algorithms, including


Bubble Sort, Selection Sort, Insertion Sort, Merge Sort, Quick Sort. The main objective
is to analyze and compare their performance based on time complexity and space complexity.
The algorithms are implemented in C programming language and tested with different types
of input data such as random, sorted, and reverse-sorted arrays. The execution time for
various input sizes is measured and compared to understand their efficiency. The study also
examines theoretical analysis using asymptotic notations like Big O. The results show that
advanced algorithms such as Merge Sort and Quick Sort perform better for large datasets,
while simpler algorithms work efficiently for small datasets. This project helps in
understanding the practical and theoretical differences between sorting techniques and their
real-world applications.

ADVANTAGES:

 Helps understand different sorting algorithms clearly.

 Shows practical comparison of time and space complexity.

 Improves programming skills in C language.

 Demonstrates how input size affects performance.

 Helps choose the best sorting algorithm for different situations.


DISADVANTAGES:

 Only basic sorting algorithms are used (no advanced modern algorithms).

 Results may change depending on the computer used.

 For small input sizes, differences between algorithms are not very clear.

 Measuring exact execution time can be difficult.

 Mostly works with simple data like numbers (not real-world complex data).

FINDINGS:

 Merge Sort and Quick Sort perform much faster than Bubble, Selection, and Insertion
Sort for large data.

 Bubble Sort and Selection Sort are the slowest for large input sizes.

 Insertion Sort works well for small or nearly sorted data.

 Quick Sort is generally the fastest on average but can be slow in the worst case.

 Merge Sort gives stable and consistent performance but uses extra memory.

 Time complexity greatly affects performance as input size increases.

TOOLS:

1. DEV C++

2. MS Word

3. Raptor

4. MS PowerPoint

ALGORITHMS:
Bubble Sort Algorithm:

Step 1: Start

Step 2: Input number of elements n

Step 3: Input array elements A[0] to A[n-1]

Step 4: Set i = 0

Step 5: Repeat while i < n-1

Step 6: Set j = 0

Step 7: Repeat while j < n-i-1

Step 8: If A[j] > A[j+1] then

Step 9: Swap A[j] and A[j+1]

Step 10: End If

Step 11: j=j+1

Step 12: End Repeat

Step 13: i = i + 1

Step 14: End Repeat

Step 15: Display sorted array

Step 16: Stop

Selection Sort Algorithm:


Step 1: Start

Step 2: Input number of elements n

Step 3: Input array elements A[0] to A[n-1]

Step 4: Set i = 0

Step 5: Repeat while i < n-1

Step 6: Set min = i

Step 7: Set j = i + 1

Step 8: Repeat while j < n

Step 9: If A[j] < A[min] then

Step 10: min = j

Step 11: End If

Step 12: j=j+1

Step 13: End Repeat

Step 14: Swap A[i] and A[min]

Step 15: i = i + 1

Step 16: End Repeat

Step 17: Display sorted array

Step 18: Stop

Insertion Sort Algorithm:


Step 1: Start

Step 2: Input number of elements n

Step 3: Input array elements A[0] to A[n-1]

Step 4: Set i = 1

Step 5: Repeat while i < n

Step 6: key = A[i]

Step 7: j=i-1

Step 8: While j >= 0 AND A[j] > key

Step 9: A[j+1] = A[j]

Step 10: j=j-1

Step 11: End While

Step 12: A[j+1] = key

Step 13: i = i + 1

Step 14: End Repeat

Step 15: Display sorted array

Step 16: Stop

Quick Sort Algorithm:


Step 1: Start

Step 2: Input number of elements n

Step 3: Input array elements A[0] to A[n-1]

Step 4: Select a pivot element from the array

Step 5: Place the pivot at its correct position in the array

Step 6: Place all smaller elements to the left of pivot

Step 7: Place all larger elements to the right of pivot

Step 8: Recursively apply Quick Sort on the left sub-array

Step 9: Recursively apply Quick Sort on the right sub-array

Step 10: Display sorted array

Step 11: Stop

Merge Sort Algorithm:

Step 1: Start

Step 2: Input number of elements n

Step 3: Input array elements A[0] to A[n-1]

Step 4: If the array has more than one element then

Step 5: Divide the array into two halves

Step 6: Call MergeSort for the left half

Step 7: Call MergeSort for the right half

Step 8: Merge the two sorted halves into a single sorted array

Step 9: End If

Step 10: Display sorted array

Step 11: Stop

FLOW CHART:
CODE:
#include <stdio.h>

#define MAX 100

void bubbleSort(int a[], int n)

int i,j,temp;

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

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

if(a[j] > a[j+1])

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

void selectionSort(int a[], int n)

int i,j,min,temp;

for(i=0;i<n-1;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;

void insertionSort(int a[], int n)

int i,j,key;

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

key = a[i];

j = i-1;

while(j>=0 && a[j] > key)

{
a[j+1] = a[j];

j = j-1;

a[j+1] = key;

void merge(int a[], int l, int m, int r)

int i,j,k;

int n1 = m-l+1;

int n2 = r-m;

int L[n1], R[n2];

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

L[i] = a[l+i];

for(j=0;j<n2;j++)

R[j] = a[m+1+j];

i=0;

j=0;

k=l;
while(i<n1 && j<n2)

if(L[i] <= R[j])

a[k] = L[i];

i++;

else

a[k] = R[j];

j++;

k++;

while(i<n1)

a[k] = L[i];

i++;

k++;

while(j<n2)

a[k] = R[j];
j++;

k++;

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

if(l<r)

int m = (l+r)/2;

mergeSort(a,l,m);

mergeSort(a,m+1,r);

merge(a,l,m,r);

int partition(int a[], int low, int high)

int pivot = a[high];

int i = low-1;

int j,temp;

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

if(a[j] < pivot)

i++;

temp=a[i];

a[i]=a[j];

a[j]=temp;

temp=a[i+1];

a[i+1]=a[high];

a[high]=temp;

return i+1;

void quickSort(int a[], int low, int high)

if(low < high)

int pi = partition(a,low,high);

quickSort(a,low,pi-1);

quickSort(a,pi+1,high);
}

void printArray(int a[], int n)

int i;

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

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

printf("\n");

int main()

int a[MAX], b[MAX], c[MAX], d[MAX], e[MAX];

int n,i;

printf("Enter number of elements: ");

scanf("%d",&n);

printf("Enter elements:\n");

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

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

b[i]=c[i]=d[i]=e[i]=a[i];

}
bubbleSort(a,n);

printf("\nBubble Sort: ");

printArray(a,n);

selectionSort(b,n);

printf("Selection Sort: ");

printArray(b,n);

insertionSort(c,n);

printf("Insertion Sort: ");

printArray(c,n);

mergeSort(d,0,n-1);

printf("Merge Sort: ");

printArray(d,n);

quickSort(e,0,n-1);

printf("Quick Sort: ");

printArray(e,n);

return 0;

OUTPUT:
Conclusion:

In this project, different sorting algorithms such as Bubble Sort, Selection Sort, Insertion
Sort, Merge Sort, and Quick Sort were studied and implemented in C. Their performance was
compared based on time complexity and efficiency. Simple algorithms are easy to understand
but slower for large data, while advanced algorithms like Quick Sort and Merge Sort are
faster and more efficient. The study shows that choosing the right sorting algorithm depends
on the size of data and program requirements.

You might also like