DS Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

A

Project on

SORTING METHODS
Submitted in fulfillment of the Laboratory assessment

Bachelor of Technology
In

Department of Computer Science and Engineering(AI&ML)


By

K.Vamshi Krishna 22841A6617

P.Karthik Reddy 22841A6638

K.Chaitanya 22841A6658

Under the esteemed guidance of


Mr.Vijay Kumar
Assistant professor

AURORA’S TECHNOLOGICAL AND RESEARCH INSTITUTE


(Accredited by NAAC with ‘A’ Grade)
(Approved by AICTE and Affiliated to JNTU, Hyderabad)
Parvathapur, Uppal, Hyderabad-500 098

(2023-24)

i
AURORA’S TECHNOLOGICAL AND RESEARCH INSTITUTE
(Accredited by NAAC with ‘A’ Grade)
(Approved by AICTE and Affiliated to JNTU, Hyderabad)
Parvathapur, Uppal, Hyderabad-500 098

2023-24

CERTIFICATE
This is to certify that the project report entitled “ SORTING METHODS” has been submitted
by K.Vamshi Krishna(22841A6617), P.Karthik Reddy(22841A6638),
K.Chaitanya(22841A6658) in fulfillment for laboratory project in of Data structures lab is a record
of bonafide work carried out by them under my guidance and supervision.

Date:
Hyderabad
Mr.Vijay Kumar

Assistant professor

Head of the Department Director


Name : Mrs. Durga Pavani Mr. DR.A.Mahesh Babu

ii
Acknowledgement

We profoundly grateful to express our deep sense of gratitude and respect towards our
guide, Mr.Vijay Kumar, Assistant professor, Department of Electrical Engineering,
AURORA'S TECHNOLOGICAL AND RESEARCH INSTITUTE, PARVATHAPUR, for his
excellent guidance right from selection of project and his valuable suggestions throughout the project work.
We are thankful to him for giving opportunity to work in the laboratory at any time. His constant
encouragement and support has been the cause for us to success, in completing this project in the college. He
has given us a tremendous support both technical and moral front.

We are thankful to Durga Pavani, HOD, Department of Computer Science Engineering, for
her valuable suggestions and support in completion of the project.

We are thankful to Mr. Dr.A.Mahesh Babu, Director, AURORA'S TECHNOLOGICAL


AND RESEARCH INSTITUTE for the support during and till the completion of the project.

We extend our thanks to College Management for their support and encouragement for
success of the project.

iii
Write a program that implements that following sorting methods to sort a given list of integers
in ascending order.

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(“ how many elements are you going to enter ?:”);
scanf(“%d”,&count);
printf(“enter %d elements:”,count);
for (i=0;i<count;i++)
scanf (“%d”,&number[i]);
quicksort (number,0, count-1);
printf(“order of sorted elements:”);
for(i=0;i<count;i++)
printf(“%d”, number [i]);
return 0;
}

Dept of CSE(AI&ML)__,ATRI
Output:
How many elements are you going to enter?: 10
Enter 10 elements: 2 3 5 7 1 9 8 0 4
Order of sorted elements:
0123345789

Heap Sort :
#include <stdio.h>
/* function to heapify a subtree. Here 'i' is the
index of root node in array a[], and 'n' is the size of heap. */
void heapify(int a[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child
int right = 2 * i + 2; // right child // If left child is larger than root
if (left < n && a[left] > a[largest])
largest = left;
// If right child is larger than root
if (right < n && a[right] > a[largest])
largest = right;
// If root is not largest
if (largest != i) {
// swap a[i] with a[largest]
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;

heapify(a, n, largest);
}
}
/*Function to implement the heap sort*/
void heapSort(int a[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
/* Move current root element to end*/
// swap a[0] with a[i]
int temp = a[0];
a[0] = a[i];
a[i] = temp;

heapify(a, i, 0);
}
}

Dept of CSE(AI&ML)__,ATRI
/* function to print the array elements */
void printArr(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d", arr[i]);
printf(" ");
}

}
int main()
{
\\ int a[] = {48, 10, 23, 43, 28, 26, 1};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}

Output

Merge Sort :
#include <stdio.h>

/* Function to merge the subarrays of a[] */


void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;

int LeftArray[n1], RightArray[n2]; //temporary arrays

/* copy data to temp arrays */


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

Dept of CSE(AI&ML)__,ATRI
LeftArray[i] = a[beg + i];
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j];

i = 0; /* initial index of first sub-array */


j = 0; /* initial index of second sub-array */
k = beg; /* initial index of merged sub-array */

while (i < n1 && j < n2)


{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}

while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}

void mergeSort(int a[], int beg, int end)


{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}

/* Function to print the array */


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

Dept of CSE(AI&ML)__,ATRI
int i;
for (i = 0; i < n; i++) printf("%d ", a[i]); printf("\n");
}

int main()
{
int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a, n);
mergeSort(a, 0, n - 1);
printf("After sorting array elements are - \n");
printArray(a, n);
return 0;
}

Output:

Dept of CSE(AI&ML)__,ATRI

You might also like