Ds Project
Ds Project
1. Modi Mansi-(1128)
2. Patel Astha-(1147)
3. Patel Dhruvi H-(1154)
4. Patel Hetvi –(1159)
There are different types of sorting techniques :
1. Selection sort
2. Insertion sort
3. Bubble sort
4. Merge sort
5. Quick sort
6. Radix sort
7. Heap sort
8. Shell sort
Selection sort
Definition:
Selection sort is a simple sorting algorithm that
works by repeatedly selecting the minimum (or
maximum) element from an unsorted portion of
the array and moving it to the beginning (or end)
of the sorted portion.
Easy to implement
In-place sort (requires no additional storage
space).
It requires no additional storage space.
It is scalable.
It is faster than any other sorting technique.
It works best for inputs which are already sorted.
Simple Implementation: Easy to understand and
implement.
In-Place Sorting: Requires only a constant
amount of additional memory(O(1) space
complexity).
Performance on Small Arrays: Works efficiently
for small arrays or lists.
It require less number of swaps (or memory
20 12 10 30 15
10 12 20 30 15
10 12 20 30 15
10 12 15 30 20
10 12 15 30 20
10 12 15 20 30
Algorithm:
1. Find the minimum value in the list .
2. Swap it with the value in the current position
3. Repeat this process for all the elements until
the entire array is sorted
4. This algorithm is called selection sort since it
repeatedly selects the smallest element.
Implementation:
void Selection(int A [], int n)
{
int i, j, min, temp;
for (i = 0; i < n - 1; i++)
{
min=i;
for (j=i+1; j < n; j++)
{
if(A [j] < A [min])
min = j;
}// swap elements
temp = A[min];
A[min] = A[i];
A[i] = temp;
}
}
Example:
Example 1:
Consider the array: [64, 25, 12, 22, 11]
1.First Pass:
Find the minimum (11), swap it with 64.
Array becomes: [11, 25, 12, 22, 64]
2.Second Pass:
Find the minimum (12), swap it with 25.
Array becomes: [11, 12, 25, 22, 64]
3.Third Pass:
Find the minimum (22), swap it with 25.
Array becomes: [11, 12, 22, 25, 64]
4.Fourth Pass:
The next minimum is already in place
(25), so just move to the next element.
5.Final Pass:
The last element (64) is already sorted.
Final Sorted Array: [11,12,22,25,64]
Example 2:
sort the array [126, 10, 9, 228, 78, 99, 56, 110,
129, 20]
1. First pass:
Find the minimum from index 0 to 9.
Minimum is 9 (at index 2).
Swap 9 with 126.
Result: [9, 10, 126, 228, 78, 99, 56, 110,
129, 20].
2. Second Pass:
Find the minimum from index 1 to 9.
Minimum is 10 (at index 1).
No swap needed.
Result: [9, 10, 126, 228, 78, 99, 56, 110,
129, 20]
3. Third Pass:
Find the minimum from index 2 to 9.
Minimum is 20 (at index 9).
Swap 20 with 126.
Result: [9, 10, 20, 228, 78, 99, 56, 110,
129, 126]
4. Fourth Pass:
Find the minimum from index 3 to 9.
Minimum is 56 (at index 6).
Swap 56 with 228.
Result: [9, 10, 20, 56, 78, 99, 228, 110, 129,
126]
5. Fifth Pass:
Find the minimum from index 4 to 9.
Minimum is 78 (at index 4).
No swap needed.
Result: [9, 10, 20, 56, 78, 99, 228, 110,
129, 126]
6. Sixth Pass:
Find the minimum from index 5 to 9.
Minimum is 99 (at index 5).
No swap needed.
Result: [9, 10, 20, 56, 78, 99, 228, 110, 129,
126]
7. Seventh Pass:
Find the minimum from index 6 to 9.
Minimum is 110 (at index 7).
Swap 110 with 228.
Result: [9, 10, 20, 56, 78, 99, 110, 228, 129,
126]
8. Eighth Pass:
Find the minimum from index 7 to 9.
Minimum is 126 (at index 9).
Swap 126 with 228.
Result: [9, 10, 20, 56, 78, 99, 110, 126, 129,
228]
9. Ninth Pass:
The remaining elements (129 and 228) are
already in order.
No further swaps needed.
Final Sorted Array:
[9, 10, 20, 56, 78, 99, 110, 126, 129, 228]
Algorithm:
1.If it is the first element, it is already sorted.
return 1;
2.Pick next element
3.Compare with all elements in the sorted sub-list
4.Shift all the elements in the sorted sub-list that is
greater than the value to be sorted
5.Insert the value
6.Repeat until list is sorted
Implementation:
void InsertionSort(int A[], int n)
{
int i, j, v;
for (i=1; i <= n - 1; i++)
{
v - A[i];
j = i;
while (A[j-1] > v && j >= 1)
{
A[j] = A[j-1];
j--;
}
A[j] = v;
}
}
Example :
Given an array: 6 8 1 4 5 3 7 2 and the goal is to
put them in ascending order.
6 8 1 4 5 3 7 2 (Consider index 0)
6 8 1 4 5 3 7 2 (Consider indices 0-1)
1 6 8 4 5 3 7 2 (Consider indices 0-2: insertion
places 1 in front of 6 and 8)
1 4 6 8 5 3 7 2 (Process same as above is
repeated until array is sorted)
14568372
13456782
1 2 3 4 5 6 7 8 (The array is sorted!)
BUBBLE SORT
Definition :
A simple algorithm that sorts a list of data by
comparing adjacent elements and swapping
them if they are out of order.
Algorithm:
1. Starting from the first element, compare it with
the second element.
2. If the first element is larger than the second,
swap them.
3. Move to the next pair of adjacent elements and
repeat the comparison and swapping process.
4. Continue this for each pair, moving from the
beginning to the end of the list.
5. After each complete pass, the largest element
"bubbles" up to its correct position (end of the
list).
6. Repeat the process for the remaining unsorted
elements, excluding the last sorted elements.
Implementation:
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
}
Example:
array: [5, 3, 8, 4, 2]
Pass 1:
Compare 5 and 3 → swap → [3, 5, 8, 4, 2]
Compare 5 and 8 → no swap → [3, 5, 8, 4, 2]
Compare 8 and 4 → swap → [3, 5, 4, 8, 2]
Compare 8 and 2 → swap → [3, 5, 4, 2, 8]
Pass 2:
Compare 3 and 5 → no swap → [3, 5, 4, 2, 8]
Compare 5 and 4 → swap → [3, 4, 5, 2, 8]
Compare 5 and 2 → swap → [3, 4, 2, 5, 8]
Pass 3:
Compare 3 and 4 → no swap → [3, 4, 2, 5, 8]
Compare 4 and 2 → swap → [3, 2, 4, 5, 8]
Pass 4:
Compare 3 and 2 → swap → [2, 3, 4, 5, 8]
Now, the array is sorted: [2, 3, 4, 5, 8].
MERGE SORT
Definition :
Merge Sort is a divide-and-conquer algorithm.
It works by recursively splitting the array into
two halves, sorting each half, and then
merging the two sorted halves back together.
Algorithm :
1.If the list has only one element, it's already
sorted.
2.Recursively split the list into two halves.
3.Sort each half by applying merge sort
recursively.
4. Merge the sorted halves.
Implementation:
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int 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 arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
Example:
Given an array: [38, 27, 43, 3, 9, 82, 10]
Split: [38, 27, 43, 3, 9, 82, 10] → [38, 27, 43] and
[3, 9, 82, 10]
Split [38, 27, 43] → [38] and [27, 43]
Split [27, 43] → [27] and [43]
Merge [27] and [43] → [27, 43]
Merge [38] and [27, 43] → [27, 38, 43]
Similarly, sort the second half [3, 9, 82, 10] → [3,
9, 10, 82]
Finally, merge [27, 38, 43] and [3, 9, 10, 82] →
[3, 9, 10, 27, 38, 43, 82]
Quick sort
Definition:
Quick sort is also an divide and conquer
algorithm.
It picks an element as a pivot and
partitions the given around the picked
pivot.
This algorithm is used to quickly sort
items within an array no matter how big
the array is.
Algorithm:
while(A[right]>pivot_item)
right--;
if(left<right)
swap(A,left,right);
}
A[low]=A[right];
A[right]=pivot_item;
Return right;
}
Example:
Given the array: [3, 6, 8, 10, 1, 2, 1]
1. Choose a pivot (e.g., 3).
2. Partition the array: [1, 2, 1, 3, 10, 8,
6] (where 3 is in its correct position).
3. Recursively sort the left part [1, 2, 1]
and the right part [10, 8, 6].
Radix sort
Definition:
Radix sort is a linear sorting algorithm
that sorts elements by processing them
digit by digit.
It is an efficient sorting algorithm for
integers or strings with fixed-size keys.
we use counting sort as a subroutine to
sort.
Algorithm:
Example:
Initial Array
[170, 45, 75, 90, 802, 24, 2, 66]
Step 1: Sort by Least Significant Digit
(1s place)
Digits:
0: (170, 90)
5: (45, 75)
2: (802, 2)
4: (24)
6: (66)
Output after sorting by 1s place:
[170, 90, 802, 2, 24, 45, 75, 66]
Step 2: Sort by Next Significant Digit
(10s place)
Digits:
0: (802, 2, 24)
4: (45)
6: (66)
7: (170, 75)
9: (90)
Algorithm:
Definition:
Shell short , also known as a shell’s method
is, a sorting algorithm that works by sorting
pair of elements that are far apart then
gradually reducing the gap between element
to be compared.
Algorithm:
1.Start with a large gap:
The gap is initially chosen as half the size of the
array.
It will reduce by half with each iteration until the
gap becomes 1.
2. Perform a gapped insertion sort:
For each gap size, sort the elements that are
gap positions apart.
Instead of comparing adjacent elements,
compare elements that are gap positions apart.
Continue this process until the gap becomes 1.
3. Repeat the process:
After each pass with a gap, reduce the gap size.
Perform the insertion sort for the new gap.
When the gap is reduced to 1, the array will be
almost sorted, and a final insertion sort will sort
the array.
Implementation:
Example:
For the array: [12, 34, 54, 2, 3]
1. Initial Array:
[12, 34, 54, 2, 3]
Gap=k/2 5/2=2
2. First Gap (gap = 2):
Compare and sort elements that are 2
positions apart:
Compare 12 and 54: no change.
Compare 34 and 2: swap -> [12, 2, 54, 34,
3]
Compare 54 and 3: swap -> [12, 2, 3, 34,
54]
3. Next Gap (gap = 1):
Perform insertion sort on the whole array:
Compare and sort as in regular insertion
sort.
4. Final Sorted Array:
[2, 3, 12, 34, 54]
REFERENCES:
https://www.geeksforgeeks.org/
https://www.javatpoint.com/
https://www.w3schools.com/
https://chatgpt.com/
THANK YOU