0% found this document useful (0 votes)
3 views

DSA ASSIGNMENT 2

The document presents an assignment on sorting algorithms, specifically Insertion Sort, Merge Sort, and Quick Sort, detailing their processes, iterative and recursive implementations, and time complexities. It includes code snippets for each algorithm and explains how they operate, such as dividing arrays in Merge Sort and selecting pivots in Quick Sort. The time complexities for each sorting algorithm are also summarized, highlighting their best and worst-case scenarios.

Uploaded by

Ubaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

DSA ASSIGNMENT 2

The document presents an assignment on sorting algorithms, specifically Insertion Sort, Merge Sort, and Quick Sort, detailing their processes, iterative and recursive implementations, and time complexities. It includes code snippets for each algorithm and explains how they operate, such as dividing arrays in Merge Sort and selecting pivots in Quick Sort. The time complexities for each sorting algorithm are also summarized, highlighting their best and worst-case scenarios.

Uploaded by

Ubaid Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

1

DSA ASSIGNMENT #2
SUBMITTED BY:
UBAID AHMAD

ROLL NO:
2410-0011

SUBMITTED TO:
SIR ZEESHAN ASLAM

DATE:
23/03/2025
2

1. Insertion Sort
Insertion sort takes one element of array at a time and insert it into its
correct position among the already sorted elements.
This process repeats for all elements until the entire array is sorted.

Iterative code:
void inser(int arr[],int size)
{
for(int i=1;i<size;i++)
{
int curr=arr[i];
int prev=i-1;
while(prev>=0&&arr[prev]>curr)
{
arr[prev+1]=arr[prev];
prev--;
}
arr[prev+1]=curr;
}
}

Recursive Code:
void recursiveInsert(int arr[],int size)
{
if(size<=1)
3

{
return;
}
recursiveInsert(arr,size-1);
for(int i=1;i<size;i++)
{
int curr=arr[i];
int prev=i-1;
while(prev>=0&&arr[prev]>curr)
{
arr[prev+1]=arr[prev];
prev--;
}
arr[prev+1]=curr;
}
}

TIME COMPLEXITY:

Sorting Average
Best Worst
Algorithm,
Case, Case, Case,

Insertion Sort , O(n) (Already Sorted) , O(n²) , O(n²)

Dry run
4
5

2. Merge Sort (Divide & Conquer)

Merge sort;
 Divide the array into two halves.

 Keep dividing until you reach single elements.

 Merge sorted halves back together.

code:
void merge(int arr[], int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;
int leftArr[n1], rightArr[n2];
for (int i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];
int i = 0, j = 0, k = left;
while (i < n1 && j < n2)
{
if (leftArr[i] <= rightArr[j])
{
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
6

}
k++;
}
while (i < n1)
{
arr[k] = leftArr[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = rightArr[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
7
8

3. Quick Sort
 It selects a pivot element and rearranges elements so that smaller ones
go to the left and larger ones to the right.

 Then, it recursively sorts the left and right parts.

CODE
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
9

}
}

TIME COMPLEXITY

END,,,

You might also like