Expt 1 DAA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Experiment:1 Date:23/01/24

TITLE: Time Complexity of Sorting algorithms

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:

Step 1 − Check if the first element in the input array is greater


than the next element in the array.

Step 2 − If it is greater, swap the two elements; otherwise move


the pointer forward in the array.

Step 3 − Repeat Step 2 until we reach the end of the array.

Step 4 − Check if the elements are sorted; if not, repeat the


same process (Step 1 to Step 3) from the last element of the
array to the first.

Step 5 − The final output achieved is the sorted array.

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:

o Select an element from the array as the pivot. Common choices


include:
▪ The first element.
▪ The last element.
▪ A random element.
▪ The middle element.

Step2: Partition the Array:

o Rearrange the array so that all elements less than or equal to


the pivot are on the left side, and all elements greater than the
pivot are on the right side.
o Use two pointers (i and j) to traverse the array from left to
right:
▪ Increment i until an element greater than or equal to the

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.

▪ Repeat until i crosses j.

Step3: Place the Pivot in Its Correct Position:

o Swap the pivot element with the element at index i + 1.


o Now the pivot is in its correct position, with smaller elements to
its left and larger elements to its right.

Step4: Recursively Sort Subarrays:

o Apply Quicksort recursively to the left subarray (elements


before the pivot) and the right subarray (elements after the
pivot).
o The base case for recursion is when the subarray has only one
element (already sorted).

Step5: Combine Sorted Subarrays:

o As the recursion unwinds, the entire array becomes sorted.

Code:
#include <iostream>
using namespace std;
int Count=0;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}

int partition(int arr[], int low, int high) {


int pivot = arr[low];
Count++;
int i = low + 1;
Count++;
int j = high;
Count++;
do {
Count++;
while (arr[i] <= pivot && i <= high) {
Count++;
i++;
Count++;
}
Count++;
while (arr[j] > pivot) {
Count++;
j--;
Count++;
}
Count++;
if (i < j) {
Count++;
swap(arr[i], arr[j]);
Count++;
}
Count++;
} while (i < j);
Count++;
swap(arr[low], arr[j]);
Count++;
return j;
Count++;
}

void QSort(int arr[], int low, int high) {


if (low < high) {
Count++;
int pivotIndex = partition(arr, low, high);
Count++;
QSort(arr, low, pivotIndex - 1);
Count++;
QSort(arr, pivotIndex + 1, high);
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;

QSort(arr, 0, n - 1);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;

cout << "Worst case:" << 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:

o Divide the input array into two equal-sized subarrays.


o Recursively apply Merge Sort to both subarrays.

Step2: Conquer (Sort):

o Continue dividing each subarray until it cannot be further


divided (i.e., it contains only one element). An array with one
element is always considered sorted.
o Sort the subarrays.

Step3: Merge:

o Merge the sorted subarrays back together to obtain the final


sorted array.
o Compare elements from both subarrays and place them in the
correct order.
o Repeat until all elements are merged.

Step4: Return:

o The entire array is now sorted.


Code:
#include<iostream>
using namespace std;
int Count=0;
void merge(int arr[],int mid,int low,int high)
{
int i,j,k;
int B[high-low+1];
i=low;
j=mid+1;
k=0;
while(i<=mid && j<=high)

{
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.

You might also like