0% found this document useful (0 votes)
27 views48 pages

Ds Project

Uploaded by

pastha209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views48 pages

Ds Project

Uploaded by

pastha209
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 48

303-DATA STRUCTURE

SORTING TECHNIQUES AND SELECTION SORT


Created by:

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.

 Selection sort is a simple sorting algorithm. This


sorting algorithm is a in-place comparison based
algorithm in which the list is divided into two
parts, sorted part at left end and unsorted part at
right end. Initially sorted part is empty and
unsorted part is entire list.

 Smallest element is selected from the unsorted


array and swapped with the leftmost element
and that element becomes part of sorted array.
This process continues moving unsorted array
boundary by one element to the right.

 Selection sort is an in-place sorting algorithm.


Selection sort works well for small files.
 It is for sorting the files with very large values
used and small keys.

 This is because selection is made based on


keys and swaps are made only when required.

Advantages of selection sort :

 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

writes) compared to many other standard


algorithms. Only cycle sort beats it in terms of
memory writes.Therefore it can be simple
algorithm choice when memory writes are costly.

Disadvantages of selection sort :


 With a time complexity of O(n²), selection sort
is inefficient for large datasets, making it
slower than more advanced algorithms like
quick sort, merge sort, or heap sort.
 Selection sort does not take advantage of
existing order of elements. It performs the
same number of operations regardless of
whether the array is partially or fully sorted.
 The algorithm is unstable by default, meaning
equal elements may not maintain their original
relative order after sorting.
 The algorithm needs better cache
performance due to its non-sequential memory
access patterns, leading to slower execution
on modern processors than algorithms like
merge sort or quick sort.
 Selection sort needs to lend itself better to
parallelization, making it less suitable for
modern multi-core processors than other
algorithms that can be parallelized effectively.

Applications of the selection sort :


Here are the top five applications of the selection
sort algorithm:

1. Selection sort is effective for sorting small


datasets where the overhead of more complex
algorithms isn't justified. Its simplicity and ease of
implementation make it suitable for educational
and small-scale applications.
2. Selection sort is an in-place sorting algorithm
with a space complexity of O(1). It's helpful in
environments with limited memory, where
minimizing additional space usage is crucial.
3. Selection sort can be advantageous in
scenarios where the cost of writing to memory is
significantly higher than reading, such as with
flash memory or EEPROM. Compared to
algorithms like bubble sort, it minimizes the
number of swaps (write operations).
4. Selection sort can be used as the initial pass
in more complex sorting algorithms. For
instance, hybrid sorting algorithms might sort a
small subset of data before applying a more
advanced algorithm like quick sort or merge sort
to the remainder.
5. Although selection sort is not the most
efficient algorithm for finding the kth smallest or
largest element, it can be used in small datasets.
By running the algorithm up to the kth iteration,
you can directly obtain the desired element
without thoroughly sorting the array.
How selection sort works?
20 12 10 30 15

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]

Example code of selection sort :


#include<stdio.h>
int main()
{
int i,a[10],n=10,pass,min_index,temp;
for(i=0;i<n;i++)
{
printf("ENTER THE VALUE [%d] : ",i);
scanf("%d",&a[i]);
}
printf("\n\aSELECTION_SORT\n");
for(pass=0;pass<n-1;pass++)
{
min_index=pass;
for(i=pass+1;i<n;i++)
{
if(a[i]<a[min_index])
{
min_index=i;
}
}
if(min_index != pass)
{
temp=a[pass];
a[pass]=a[min_index];
a[min_index]=temp;
}
}
for(i=0;i<n;i++)
{
printf("\n\t%d",a[i]);
}
return 0;
}
Insertion sort
Definition:
 Insertion sort is a simple and efficient
comparison sort
 . In this algorithm, each iteration removes an
element from the input data and inserts it into
the correct position in the list being sorted.
 The choice of the element being removed from
the input is random and this process is
repeated until all input elements have gone
through.

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:

1. Choose a pivot element.


2. Partition array into:

2.1 Left (elements < pivot)


2.2 Middle (elements == pivot)
2.3 Right (elements > pivot)

3.Recursively apply quicksort to Left and


Right.

4.Combine sorted Left, Middle, and Right.


Implementation:
Void quicksort(int A[],int low, int high)
{
Int pivot;
If (high > low)
{
Pivot=Partition(A , low , high);
quicksort(A , low , pivot-1);
quicksort(A,pivot+1,high);
}
}
Int Partition(int A, low, int high) {
Int left, right, pivot_item=A[low];
Left=low;
Right=high;
While(left<right){
while(A[left]<=pivot_item)
left++;

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:

1. Find maximum element's number of


digits (k).
2. Initialize 10 buckets (0-9).
3. Iterate through each digit position
(from least significant):

3.1 Distribute elements into buckets based


on current digit.
3.2 Collect elements from buckets,
maintaining relative order.
4.Repeat step 3 for k digit positions.
Implementation:
void countingSort(int arr[], int n, int exp)
{
int output[n];
int count[10] = {0};
in count[]
for (int i = 0; i < n; i++)
{
int index = (arr[i] / exp) % 10;
count[index]++;
}
for (int i = 1; i < 10; i++)
{
count[i] += count[i - 1];
}

for (int i = n - 1; i >= 0; i--)


{
int index = (arr[i] / exp) % 10;
output[count[index] - 1] = arr[i];
count[index]--;
}
for (int i = 0; i < n; i++)
{
arr[i] = output[i];
}
}
void radixSort(int arr[], int n)
{
int max = arr[0];
for (int i = 1; i < n; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
for (int exp = 1; max / exp > 0; exp *= 10)
{
countingSort(arr, n, exp);
}
}

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)

Output after sorting by 10s place:


[802, 2, 24, 45, 170, 75, 66, 90]
Step 3: Sort by Most Significant Digit
(100s place)
Digits:
 0: (45, 75, 90, 24, 2)
 1: (170)
 8: (802)

Output after sorting by 100s place:


[2, 24, 45, 66, 75, 90, 170, 802]

Final Sorted Array:


Sorted array: [2, 24, 45, 66, 75, 90, 170,
802]
Heap sort
Definition:
 Heap Sort is a comparison-based sorting
technique based on the binary heap data
structure. A Binary Heap is a complete
binary tree, where each parent node is
greater than or equal to (Max Heap) or
less than or equal to (Min Heap) its child
nodes.
 Heap Sort uses a Max Heap to sort
elements in increasing order by first
building a heap, then repeatedly
extracting the maximum element from the
heap and reconstructing the heap.

Algorithm:

1. Build a Max Heap: Construct a Max


Heap from the given array. The largest
element will be the root.
2. Swap Root with Last Element: Swap
the root (maximum element) with the
last element of the heap.
3. Reduce Heap Size: Remove the last
element from the heap (it is now
sorted).
4. Heapify: Call heapify() on the reduced
heap to ensure the heap property is
maintained.
5. Repeat: Repeat the process until the
heap size reduces to 1.
SHELL SORT

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:

void shellSort(int arr[], int n) {


for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] >
temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}

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

You might also like