Professional Documents
Culture Documents
Expt 1 DAA
Expt 1 DAA
Expt 1 DAA
Aim:- To Implement the following program using array data structure and analyze it’s
time complexity.
a) Insertion sort
b) Selection sort
c)Bubble sort
d)Quick sort
e) Merge sort.
Theory:
a) INSERTION SORT:
-> It builds the final sorted array one element at a time by repeatedly taking
the next element and inserting it into the correct position among the already
sorted elements.
Algorithm:
Step1: First element is already sorted return 1.
Step2: Pick next element.
Step3: Compare it with all elements in sorted sub list.
Step4: Shift all the elements in the sorted sub list that is greater than the
value to be sorted.
Step5: Insert the value.
Step6: Repeat until the list is sorted.
Code:
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
int j=0;
int count=0;
for (int i = 1; i < n; i++)
{
count++;
int key = arr[i];
count++;
for( j=i-1;j >= 0 && arr[j] >key;j--)
{
count++;
arr[j + 1] = arr[j];
count++;
}
count++;
arr[j + 1] = key;
count++;
}
count++;
cout<<"Time complexity of the sort:"<<count<<endl;
}
int main() {
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int arr1[]={10,9,8,7,6,5,4,3,2,1};
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"Best case:"<<endl;
insertionSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout<<"Worst case:"<<endl;
insertionSort(arr1, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
return 0;
}
Output:
Best case:
Time complexity of the sort:37
Sorted array: 1 2 3 4 5 6 7 8 9 10
Worst case:
Time complexity of the sort:127
Sorted array: 1 2 3 4 5 6 7 8 9 10
b) SELECTION SORT:
->It repeatedly selects the minimum (or maximum) element from the
unsorted portion of the array and places it at the beginning (or end) of the
sorted portion.
Algorithm:
Step1: Initialize the size of array, after then run a loop to check the iteration.
Step2: Take a variable name min_index and assign it as the value of the
iteration.
Step3: Again, run a loop to check if the element in the array is less than the
element at min_index
Step4: If there is any then it will set min_index to the index value of the
lowest element.
Step6: Repeat from step 3 to 4.
Step6: Swap the element at min_index with the element of the sorted index
Step7: Repeat from step 1 to 6 until the array is sorted.
Code:
#include<iostream>
using namespace std;
void Ssort(int Arr[],int n)
{
int Count=0,mid_i=0,i,j,temp;
for(i=0;i<n-1;i++)
{
Count++;
mid_i=i;
Count++;
for(j=i+1;j<n;j++)
{
Count++;
if(Arr[j]<Arr[mid_i])
{
Count++;
mid_i=j;
Count++;
}
Count++;
}
Count++;
temp=Arr[mid_i];
Count++;
Arr[mid_i]=Arr[i];
Count++;
Arr[i]=temp;
Count++;
}
Count++;
cout<<"Time complexity of the sort:"<<Count<<endl;
}
int main() {
int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int arr1[]={15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"Best case:"<<endl;
Ssort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout<<"Worst case:"<<endl;
Ssort(arr1, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
return 0;
}
Output:
Best case:
Time complexity of the sort:295
Sorted array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Worst case:
Time complexity of the sort:407
Sorted array: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
c) Bubble Sort:
->Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in the wrong order. This
algorithm is not suitable for large data sets as its average and worst-case
time complexity is quite high.
Algorithm:
Code:
#include<iostream>
using namespace std;
void Bsort(int array[],int n)
{
int Count=0;
int I,J,temp;
for(I=0;I<n-1;I++)
{
Count++;
for(J=0;J<n-I-1;J++)
{
Count++;
if(array[J]>array[J+1])
{
Count++;
temp=array[J];
Count++;
array[J]=array[J+1];
Count++;
array[J+1]=temp;
Count++;
}
Count++;
}
Count++;
}
Count++;
cout<<"Time complexity of the sort:"<<Count<<endl;
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int arr1[]={10,9,8,7,6,5,4,3,2,1};
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"Best case:"<<endl;
Bsort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout<<"Worst case:"<<endl;
Bsort(arr1, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
return 0;
}
Output:
Best case:
Time complexity of the sort:109
Sorted array: 1 2 3 4 5 6 7 8 9 10
Worst case:
Time complexity of the sort:289
Sorted array: 1 2 3 4 5 6 7 8 9 10
d)Quick sort:
-> Quicksort is a sorting algorithm based on the Divide and Conquer algorithm
that picks an element as a pivot and partitions the given array around the
picked pivot by placing the pivot in its correct position in the sorted array.
Algorithm:
Step1: Choose a Pivot:
pivot is found.
▪ Decrement j until an element less than or equal to the
pivot is found.
▪ Swap the elements at indices i and j.
Code:
#include <iostream>
using namespace std;
int Count=0;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int arr1[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);
QSort(arr, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
QSort(arr1, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
return 0;
}
Output:
Best case:
Time complexity of the sort:226
Sorted array: 1 2 3 4 5 6 7 8 9 10
Worst case:
Time complexity of the sort:452
Sorted array: 1 2 3 4 5 6 7 8 9 10
e) MERGE SORT:
->Merge sort is a popular sorting algorithm that follows the divide-and-conquer
approach to sort elements in an array or a list. It divides the array into smaller
sub-arrays, sorts them individually, and then merges them back together to
produce a sorted array. Merge sort is efficient and stable, with a time complexity
of O(n log n) for both average and worst-case scenarios.
Algorithm:
Step1: Divide:
Step3: Merge:
Step4: Return:
{
Count++;
if(arr[i]<arr[j])
{
Count++;
B[k]=arr[i];
Count++;
i++;
Count++;
k++;
Count++;
}
Count++;
if(arr[i]>arr[j])
{
Count++;
B[k]=arr[j];
Count++;
j++;
Count++;
k++;
Count++;
}
Count++;
}
while(i<=mid)
{
Count++;
B[k]=arr[i];
Count++;
i++;
Count++;
k++;
Count++;
}
Count++;
while(j<=high)
{
Count++;
B[k]=arr[j];
Count++;
j++;
Count++;
k++;
Count++;
}
Count++;
for (i = low, k = 0; i <= high; i++)
{
Count++;
arr[i] = B[k];
Count++;
k++;
Count++;
}
Count++;
}
void mergesort(int arr[],int low,int high){
if(low<high)
{
Count++;
int mid=(low+high)/2;
Count++;
mergesort(arr ,low,mid);
Count++;
mergesort(arr ,mid+1,high);
Count++;
merge(arr ,mid,low,high);
Count++;
}
Count++;
cout<<"Time complexity of the sort:"<<Count<<endl;
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8,9,10};
int arr1[]={10,9,8,7,6,5,4,3,2,1};
int n = sizeof(arr) / sizeof(arr[0]);
cout<<"Best case:"<<endl;
mergesort(arr,0,n-1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
cout<<"Worst case:"<<endl;
mergesort(arr1,0,n-1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
return 0;
}
Output:
Best case:
Time complexity of the sort:386
Sorted array: 1 2 3 4 5 6 7 8 9 10
Worst case:
Time complexity of the sort:760
Sorted array: 1 2 3 4 5 6 7 8 9 10
Conclusion: Above all programs were implemented using array data structure and
analyzed it’s time complexity.