C++

Selection Sort in C++

Similar to many other sorting techniques, the selection sort is a stable sort that picks the lowest value from an unordered list and puts it at the first position of the unordered list in each loop. The array is fragmented into two splits via the selection sort methodology. Entire items are arranged in one section, while the objects are unordered in the other. We begin by extracting the array’s maximum and minimum values. We set the data (say, minimal) at the number one spot by substituting the data from the first position with the least data. The array shrinks as a result of the action. Let’s start with creating and opening a new C++ file in the terminal.

Example 01:

We will start our first example with a simple illustration of the conventional method to use selection sort in C++. We have added the single “iostream” header at the first line using the “#include” keyword. Use the namespace “std” via the C++ “using” shortcut. Here comes the “swap” function for selection sort to swap some values with each other. This function takes two values as an input, i.e., “x” and “y”. The variable “temp” is declared as an integer. After this, the value of variable “x” has been passed to the variable “temp”. The value of variable “y” is overridden in the variable “x”, and the value of “temp is assigned to the variable “y”. By using this swapping technique, the values of “x” and “y” variables are exchanged or swapped.

The show() function will be called by the main() method twice, i.e., before and after the sorting. This function will take array “A” and its size “n” from the main() method and iterate the values of an array “A” using the “for” loop. On each iteration, it will continue to display each value of array “A” at index “I” using the “cout” standard output statement. After displaying all the values using the “for” loop, the show() function will add a line break or end using the “\n” character within the “cout” standard statement. The show() function is completed here:

Now, it’s time to perform the selection sort on our random unordered array inputted by a user. So, this function will be called by the main() function only. It is taking the array “A” and its size “s” from the main() method. A variable “im” of integer type is initialized to be used as an index of the smallest value in the array. The outer “for” loop has been used to iterate the indexes of an array up to size “s-1”. The smallest index, “I”, will be assigned to the “im” variable. The inner “for” loop is starting from the successor of the “I” index, i.e., “j=i+1”.

On each iteration, the value of variable “im”, which is the smallest value in an array, will be compared with the index “j” of an array. If the value of “im” is less than the value of “j”, the index of “j” will be assigned to the variable “im”. Now, “j” will become “im”, and “im” will become “j”, i.e., the smallest value will be assigned to the “im” variable. The “swap” function is here to perform the swapping between the index “I” value and the value at “im” of an array, i.e., between small and large values.

Starting from the main() method, we have declared an integer variable “n”. The “cout” object is used here to ask a user to enter “the total number of elements” they want to use for an array. The “cin” standard input clause is used to get the input number from the user and save it to the variable “n”. After this, we have initialized an integer type array “A” of size “n”, i.e., size will be defined by the user input. The cout standard output statement asks users to input values in the array up to the required size, i.e., “n”. The “for” loop has been started to iterate the indexes of an array from 0 to size n-1.

On each loop iteration, the user will add a single value separated from other values by a space. After the insertion of all the values in the array “A”, the cout statement for standard output will show us that the program will display the random array added by the user itself. The show() function will be called by passing the random array “A” into it along with the size “n”. The unsorted array added by the user will be displayed on our shell.

After this, another cout statement states that the program will display the sorted array “A” after using the selection sort on the unordered one. The sort function has been called by passing the array “A” and size “n” as an argument to it. The sort function will perform a selection sort to organize the array in ascending order and update the array. After the sort function, the show() function has been called again by passing the array “A” and size “n” to its parameters. The show() function will display the sorted array “A” on the shell terminal at the end.

The code file has been compiled and executed on the shell. The user has entered the number of elements for the array as 10. After this, a user has entered the random unsorted 10 values for an array. The unsorted and the sorted array were displayed after that.

Example 02:

The selection sort can be implemented in another way without using any user-defined functions for sorting. So, we have decided to implement the other way too. It started the code with the iostream library and namespace for C++. The main() function has been started by declaring integer variables like n, array A, temp, s, check c, and index. The program has asked to enter the size. The “cin” statement is used to take size as input in variable “n”. After this, the user has asked to input 10 values.

The “for” loop has been initialized to add the 10 values using the “cin” object. The next “for” loop is used here to initialize the check value to 0 at every iteration and save the value at index “I” of an array to the variable “s”. The inner “for” loop will use the “if” statement to check whether the value at the predecessor “j” is less than value “s”. It will add the value or index “j” to variable “s”. The check will be increment, and the variable “index” will be assigned with the index “j”. If the check “c” is not equal to 0, the values will be swapped. At last, the sorted array will be printed out.

We added 8 as the input size and 8 values in the array on running this code. The sorted array has been displayed on the shell as shown below:

Conclusion:

This article was about using selection sort in C++ to sort an array in ascending order. We have used the user-defined swap, sort, and display functions to elaborate this concept more clearly and within parts. Using this article, you will be able to learn the selection sort more efficiently and effectively. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.