Selection Sort in Ascending Order in C

1. Introduction

Selection Sort is a simple comparison-based sorting algorithm. The primary idea behind Selection Sort is to divide the input list into two parts: a sorted section and an unsorted section, then repeatedly pick the smallest (or largest, depending on the sorting order) element from the unsorted subarray and swap it with the first unsorted element, moving the boundary between the two subarrays one element to the right.

2. Program Overview

The program structure is as follows:

1. swap: A utility function to exchange two integer values.

2. selectionSort: The primary function that implements the Selection Sort algorithm in ascending order.

3. printArray: A function to display the content of the array.

4. main: The primary function from where the execution begins.

3. Code Program

#include <stdio.h>

// Utility function to swap two integers
void swap(int *xp, int *yp) {
    int temp = *xp;
    *xp = *yp;
    *yp = temp;
}

// Implementation of selectionSort to sort in ascending order
void selectionSort(int arr[], int n) {
    int i, j, min_idx;

    // One by one move boundary of unsorted subarray
    for (i = 0; i < n-1; i++) {
        // Find the minimum element in unsorted array
        min_idx = i;
        for (j = i+1; j < n; j++) {
            if (arr[j] < arr[min_idx]) {
                min_idx = j;
            }
        }

        // Swap the found minimum element with the first element
        swap(&arr[min_idx], &arr[i]);
    }
}

// Function to print the array
void printArray(int arr[], int size) {
    int i;
    for (i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

// Main function to test the selectionSort function
int main() {
    int arr[] = {64, 25, 12, 22, 11};
    int n = sizeof(arr)/sizeof(arr[0]);
    selectionSort(arr, n);
    printf("Sorted array in ascending order: \n");
    printArray(arr, n);
    return 0;
}

Output:

Sorted array in ascending order:
11 12 22 25 64

4. Step By Step Explanation

The selectionSort function works step by step as follows:

1. Start from the first element and search for the smallest (minimum) element in the array, and swap it with the first element.

2. Then, move to the second element and search for the smallest element from the second element to the last element and swap it with the second element.

3. Continue this process until the entire array is sorted.

4. Each time, the "minimum" element from the unsorted subarray is picked and moved to the sorted subarray.

By the end of the algorithm, the entire array will be sorted in ascending order.

Related C Programs on Sorting Algorithms

  1. Bubble Sort in Ascending Order in C
  2. Bubble Sort in Descending Order in C
  3. Selection Sort in Ascending Order in C
  4. Selection Sort in Descending Order in C
  5. Insertion Sort in Ascending Order in C
  6. Insertion Sort in Descending Order in C
  7. Merge Sort in Ascending Order in C
  8. Merge Sort in Descending Order in C
  9. Quick Sort in Ascending Order in C
  10. Quick Sort in Descending Order in C
  11. Heap Sort in Ascending Order in C
  12. Heap Sort in Descending Order in C


Comments