Aim: To implement and demonstrate the Bubble Sort algorithm, which repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
Theory: Bubble Sort is a simple comparison-based sorting algorithm. It works by moving the largest (or smallest) unsorted element to its correct position at the end of the unsorted portion of the list in each pass.
- It makes multiple passes through the list.
- In each pass, adjacent elements are compared, and if they are out of order, they are swapped.
- After the first pass, the largest element "bubbles up" to the end of the array. The time complexity is
$\mathcal{O}(n^2)$ .
Algorithm:
- Input: The program prompts the user to enter the number of elements and the array values.
- Define
swapfunction: A helper functionswapis defined to exchange the values of two integers using pointers. - Sorting Loop: An outer
whileloop iteratesntimes (wherenis the number of elements), representing the passes. - Comparison and Swap: An inner
forloop iterates through the unsorted part of the array, comparingarray[i]witharray[i+1]. - If
array[i] > array[i+1], theswapfunction is called to exchange their values. - The outer loop counter
nis incremented after each pass, reducing the size of the unsorted portion. - Output: The sorted array is printed.
Aim: To implement and demonstrate the Insertion Sort algorithm, which builds the final sorted array one item at a time.
Theory: Insertion Sort works by treating the array as two parts: a sorted subarray and an unsorted subarray.
- It iterates through the unsorted elements, taking one element at a time.
- This element (
key) is compared with the elements in the sorted subarray. - Elements larger than the
keyin the sorted subarray are shifted one position to the right to make space. - The
keyis then inserted into its correct sorted position. The average and worst-case time complexity is$\mathcal{O}(n^2)$ , but it is efficient for small datasets or nearly sorted data.
Algorithm:
- Define
insertionSortfunction: The function takes the arrayarrand its sizen. - Outer Loop: A
forloop iterates from the second element (i=1) to the end of the array. The element at indexiis thekeyto be inserted. - Inner Loop (Shifting): A
whileloop compareskeywith elements in the sorted subarray (arr[j]). - If an element
arr[j]is greater thankey, it is shifted one position to the right (arr[j+1] = arr[j]). - The
jindex is decremented (j--) to continue shifting to the left. - Insertion: The
keyis finally placed into its correct spot atarr[j+1]. - In
main: The array is initialized, sorted usinginsertionSort, and both the original and sorted arrays are printed.
Aim: To implement and demonstrate the Selection Sort algorithm using pointer arithmetic for finding the smallest element and placing it at the correct position.
Theory: Selection Sort divides the array into two parts: a sorted part at the beginning and an unsorted part remaining at the end.
- It repeatedly finds the minimum element from the unsorted part.
- It then swaps the minimum element with the element at the beginning of the unsorted part, thereby extending the sorted part.
- The algorithm performs
$n$ passes, and its time complexity is$\mathcal{O}(n^2)$ .
Algorithm:
- Input: The program prompts the user for the array elements.
- Define
s_sortfunction: The function takes a pointer to the array (int* a) and the number of elements. - Outer Loop (Passes): A
whileloop iterateselementstimes. In each iteration, the starting position of the unsorted segment is advanced by one (by incrementing pointera). - Inner Loop (Comparison): A
forloop iterates through the rest of the unsorted array using pointerb. - Comparison and Swap: Inside the inner loop, it compares the value pointed to by
a(the current minimum candidate) with the value pointed to byb. If the current minimum candidate (*a) is greater than the element being checked (*b), theswapfunction is called to exchange them. Note: In a standard selection sort, the swap is usually performed only once per pass, outside the inner loop, after finding the true minimum. - Output: The sorted array is printed.
These experiments successfully implemented and demonstrated three foundational comparison-based sorting algorithms: Bubble Sort, Insertion Sort, and Selection Sort. All three algorithms have a time complexity of