Introduction
Selection Sort is a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted part of the array and placing it at the beginning. This process continues until the entire array is sorted. In C, you can implement Selection Sort using pointers to directly manipulate the elements of the array. This guide will show you how to write a C program to implement Selection Sort using pointers.
Example:
- Input: Array
[29, 10, 14, 37, 14]
- Output: Sorted array
[10, 14, 14, 29, 37]
Problem Statement
Create a C program that:
- Takes an array of integers as input from the user.
- Uses pointers to implement the Selection Sort algorithm to sort the array.
- Displays the sorted array.
Solution Steps
- Include the Standard Input-Output Library: Use
#include <stdio.h>
for standard input-output functions. - Declare the Array and Pointer Variables: Declare an integer array to store the elements and pointers for accessing and manipulating the elements.
- Input the Array Elements: Use a loop to take input for the array elements from the user.
- Implement Selection Sort Using Pointers: Use pointers to traverse the array, find the minimum element, and swap it with the first unsorted element.
- Display the Sorted Array: Use
printf
to display the sorted array.
C Program to Implement Selection Sort Using Pointers
#include <stdio.h> int main() { // Step 2: Declare the array and pointer variables int arr[100], n; int *ptr1, *ptr2, *min_ptr, temp; // Step 3: Input the number of elements and the array elements printf("Enter the number of elements: "); scanf("%d", &n); printf("Enter the elements of the array: "); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // Step 4: Implement Selection Sort using pointers for (int i = 0; i < n - 1; i++) { ptr1 = &arr[i]; min_ptr = ptr1; // Assume the current element is the minimum for (int j = i + 1; j < n; j++) { ptr2 = &arr[j]; if (*ptr2 < *min_ptr) { min_ptr = ptr2; // Update min_ptr if a smaller element is found } } // Swap the found minimum element with the current element if (min_ptr != ptr1) { temp = *ptr1; *ptr1 = *min_ptr; *min_ptr = temp; } } // Step 5: Display the sorted array printf("Sorted array: "); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } printf("\n"); return 0; // Return 0 to indicate successful execution }
Explanation
Step 2: Declare the Array and Pointer Variables
- The integer array
arr
is declared to store the elements of the array. - The pointers
ptr1
,ptr2
, andmin_ptr
are used to traverse and compare elements in the array. - The variable
temp
is used for swapping elements during the sorting process.
Step 3: Input the Array Elements
- The program prompts the user to enter the number of elements in the array and stores this value in
n
. - A
for
loop is used to take input for each element of the array.
Step 4: Implement Selection Sort Using Pointers
- The program uses a nested
for
loop to implement the Selection Sort algorithm:- The outer loop iterates through each element of the array, starting with the first element.
ptr1
is initialized to point to the current element, andmin_ptr
is initialized to point to the same element, assuming it is the minimum.- The inner loop iterates through the remaining elements of the array, starting from the element after
ptr1
. ptr2
is used to point to each subsequent element, and if an element smaller than the one pointed to bymin_ptr
is found,min_ptr
is updated to point to that smaller element.- After the inner loop completes, the smallest element is swapped with the element at the position of
ptr1
if it is not already the minimum.
Step 5: Display the Sorted Array
- The program uses a
for
loop to display the sorted array.
Return 0
- The
return 0;
statement indicates that the program executed successfully.
Output Example
Example Output:
Enter the number of elements: 5 Enter the elements of the array: 29 10 14 37 14 Sorted array: 10 14 14 29 37
Conclusion
This C program demonstrates how to implement Selection Sort using pointers. It covers basic concepts such as pointer manipulation, array traversal, and sorting algorithms, making it a useful example for beginners learning C programming.