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

Sorting Algorithm Report

Course assignment on data structures in c

Uploaded by

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

Sorting Algorithm Report

Course assignment on data structures in c

Uploaded by

sarahpeace450
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

EKOLE SERAH MALOBA

FE23A043

CEF 341 Algorithm and Data structures

Dr. Tsague Aline

Implementation of Sorting Algorithms

1. Simple Selection sort: It works by repeatedly finding the smallest


element in an unsorted list and swapping it with the first unsorted element.
This process continues until the list is sorted.

Algorithm:

⦁ Set MIN to location 0

⦁ Find the minimum element in the list

⦁ Swap the minimum element with the value at location MIN

⦁ Increment MIN to point to the next element

⦁ Repeat until the list is sorted

Below is the code snippet and result for a sample data arr[]= {15, 9, 29, 3,
10, 0}

Recursive:

Base Case: When the unsorted array contains only one element, Therefore
the array is already sorted. Return the function
Recursive case: Repeatedly call the function with smaller values of n,
following the same algorithm for iterative version. Below is the code snippet
and result for a sample data arr[]= {15, 9, 29, 3, 10, 0}

2. Bubble sort: It works by


repeatedly swapping the adjacent elements
if they are in the wrong order. (ascending or
descending)
The Algorithm is as follows:

⦁ Start at the beginning of the list.


⦁ Compare each pair of adjacent elements.
⦁ If the elements are in the wrong order, swap them.
⦁ Move to the next pair and repeat step 3 until the end of the list.
⦁ After each pass through the list, the largest element moves to its
correct position.
Below is the iterative code and result for sample data arr[]= {15, 9, 29, 3,
10, 0}
In ascending and descending orders
Recursion:

Base case: When the unsorted array contains only one element, Therefore
the array is already sorted. Return the function
Recursive case: Repeatedly call the function with smaller values of n,
following the same algorithm for iterative version. Below is the code snippet
and result for a sample data arr[]= {15, 9, 29, 3, 10, 0
3. Sequential Insertion Sort:

Algorithm:

 Start with the second element in the array, as the first element is already
sorted.
 Compare the selected element to each element in the sorted part of the
array.
 Move any elements that are larger than the selected element one position to
the right.
 Place the selected element in the correct position.
 Repeat the process until all elements are sorted.
Below is the code snippet and result for a sample data arr[]= {15, 9, 29, 3,
10, 0}
4. Binary Insertion Sort:
Algorithm:
 Iterate the array from the second element to the last element.
 Store the current element A[i] in a variable key.
 Find the position of the element just greater than A[i] in the subarray
from A [0] to A[i-1] using binary search. Say this element is at index
pos.
 Shift all the elements from index pos to i-1 towards the right.
 A[pos] = key.

Below is the code snippet and result for a sample data arr[]= {15, 9, 29, 3,
10, 0

You might also like