Ilovepdf Merged
Ilovepdf Merged
On
SUBMITTED BY:
Dr.LAVANYA
ASSISTANT PROFESSOR
KL UNIVERSITY
Green fields, Vaddeswaram – 522 502
Guntur Dt., AP, India.
DEPARTMENT OF BASIC ENGINEERING SCIENCES
CERTIFICATE
Dr.LAVANYA Dr.D.HARITHA
ASSISTANT PROFESSOR
ACKNOWLEDGEMENTS
I express sincere gratitude to our Coordinator and HOD-BES Dr. D. Haritha for
her leadership and constant motivation provided in successful completion of our
academic semester. I record it as my privilege to deeply thank for providing us
the efficient faculty and facilities to make our ideas into reality.
2200033069 M.Akshaya
2200033070 M.Deva Sainath Reddy
2200033071 R.Navadeep Sai
2200033074 P.Satyanarayana Chowdary
ABSTRACT
Sorting is used for arranging the data in some sequence like increasing
or decreasing order. I have discussed about various sorting algorithm with their
comparison to each other in basis of time complexity and space complexity i n C .
These papers also show running time of algorithm with the help of C language. I have
compared some types of sorting algorithm like insertion sort, selection sort, quick sort,
and bubble sort by comparing time complexity and space complexity.
1 Introduction 1-2
4 Implementation 5-14
5 Output/Screenshots 15-20
6 Conclusion 21
Page 1
INTRODUCTION
Sorting
Let p be a list of m elements P1, P2, P3…….Pn in memory. Sorting P means
arranging the content of P in either increasing or decreasing order
i.e.,P1<P2<P3<P4………..<Pn. There are m elements in the list, therefore there is
m! ways to arrange them. B. Sorting Algorithm Sorting algorithm is an important
task for arranging the elements in the list. Comparing the various types of sorting in
this paper on the basis of C and Java.
Bubble Sort
In Bubble sort, each element is compared with its adjacent element. If the first
element is larger than the second one then the position of the element is interchanged,
other it is not changed. Then next element is compared with its adjacent element and
the same process is repeated for all the elements in the array. In bubble sort, the first
pass requires (n-1) comparison to fix the highest element to its location , the second
pass requires (n-2),……,ith pairs requires (n-i) and the last pass requires only one
comparison to be fixed at its proper position. Therefore the total no. of comparisons
are: T(n)= (n-1)+(n-2)+(n-3)+……..+(n-i)+3+2+1=n(n-1)/2 T(n)=O(n^2).The
execution time of Bubble Sort in C is more as compared to Java.
Selection Sort
In selection sort, the first element of array is compared with minimum value of
array and interchanged the position of element. Then element is compared with the next
minimum value of array and the same process is repeated for all elements in the array.
In selection sort makes first pass in (n-1) comparisons, the second pass in (n-2)
comparisons and so on.Total no. of comparison are: T(n)=(n-1)+(n-2)+(n-
3)+,……………,+(n-i)+3+2+1=n(n-1)/2 T(n)=O(n^2) The execution time of
Selection Sort in C is more as compared to Java.
Quick Sort
Quick sort works by partitioning methods for sorting the array. And each
Page 2
AIM
Advantages –
Disadvantages –
Future enhancements –
➢ This could help in evaluating the all types of Sorting Algorithms by which
they could easily understand the pros and cons of Sorting algorithms and
also to find the application of these Algorithms in different areas.
Page 4
SYSTEM REQUIREMENTS
➢ SOFTWARE REQUIREMENTS:
The major software requirements of the project are as follows:
Language : Object Oriented Programming
Operating system : Windows Xp or later.
➢ HARDWARE REQUIREMENTS:
The hardware requirements that map towards the software are as follows:
RAM : 8 GB RAM
----------------"BUBBLE SORT"-------------
#include<stdio.h>
void print(int a[], int n) {
int i;
for(i = 0; i < n; i++){
printf("%d ",a[i]);
}
}
void bubble(int a[], int n){
int i, j, temp;
for(i = 0; i < n; i++) {
for(j = i+1; j < n; j++){
if(a[j] < a[i]){
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}
void main (){
int n,a[100],i;
printf("Enter no.of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Before sorting - \t");
print(a, n);
bubble(a, n);
printf("\nAfter sorting - \t");
print(a, n);
}
-----------------"SHELL SORT"----------------
#include <stdio.h>
void shellSort(int a[], int n) {
for (int k = n / 2; k> 0; k /= 2) {
for (int i = k; i < n; i += 1) {
int temp = a[i];
int j;
for (j = i; j >= k && a[j - k] > temp; j-= k) {
a[j] = a[j - k];
}
a[j] = temp;
}
}
}
void printArray(int a[], int s) {
for (int i = 0; i < s; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
int main(){
int i,n;
printf(" Enter the number of elements : " );
scanf("%d",&n);
int a[n];
for( i=0 ; i<n ; i++ ) {
printf("Enter the element %d :\t",i+ 1);
scanf(" %d",&a[i]);
}
shellSort(a,n);
printf( "Sorted Array : \n" );
printArray(a,n);
}
---------------"INSERTION SORT"--------------
#include<stdio.h>
void insertion_sort(int arr[],int n){
int i,j,temp;
for(i=1;i<n;i++){
temp = arr[i];
j=i-1;
while(j>=0 && arr[j]>temp){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
int main(){
int i,n;
printf("enter the range of elements : ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++){
printf("enter the element %d : ",i+1);
scanf("%d",&arr[i]);
}
insertion_sort(arr,n);
for(i=0;i<n;i++){
printf("%d\t",arr[i]);
}
return 0;
}
---------------"QUICK SORT"-------------
#include <stdio.h>
int partition (int a[], int start, int end){
int pivot = a[end];
int i = (start - 1);
for (int j = start; j <= end - 1; j++){
if (a[j] < pivot){
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}
void quick(int a[], int start, int end){
if (start < end){
int p = partition(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
void printArr(int a[], int n){
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main(){
int i,n;
printf(" Enter the number of elements: ");
scanf("%d",&n);
int a[n];
for( i=0 ; i<n ; i++ ){
printf("Enter the element %d:\t",i+1);
scanf("%d",&a[i]);
}
quick(a,0,n- 1);
printf( "Sorted Array : \n" );
printArr(a,n);
}
--------------"MERGE SORT"---------------
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[], 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] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r){
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size){
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
int main(){
int i,n;
printf(" Enter the no.of elements : " );
scanf("%d",&n);
int arr[n];
for( i=0 ; i<n ; i++ ) {
printf("Enter the element %d : ",i+1);
scanf(" %d",&arr[i]);
}
mergeSort(arr, 0, n - 1);
printf("\nSorted array is \n");
printArray(arr, n);
return 0;
}
Screen Shots:
Page 19
Page 20
Page 21
Page 22
Page 23
Page 24
CONCLUSION
In this study I have studied about various sorting algorithm and comparison on
the basis of time complexity, execution time and C & Java languages. I used to the C
and Java program for finding the execution time in second. I observe that when
compare all the sorting algorithms to each other then find the execution time of quick
sort algorithm is best to others and also observe that the execution time of all sorting
algorithms in java is best .