Database Programming: Selection Sort Algorithm Explained in Detail

Verified

Added on  2019/10/01

|7
|863
|325
Homework Assignment
AI Summary
This blog post provides a comprehensive explanation of the Selection Sort algorithm, specifically within the context of database programming. It begins by defining the algorithm as a method for sorting values, particularly in ascending order, through iterative comparisons and interchanges. The post details the process, highlighting how the smallest element is identified and swapped into its correct position in each pass. The post includes a code example written in C++ to illustrate the implementation of the selection sort algorithm. The post explains the step-by-step process of the selection sort algorithm. It also provides a flowchart of the algorithm. The content aims to clarify the algorithm's functionality, making it accessible for students learning about sorting techniques in database programming. The blog post also includes a step-by-step example to illustrate the process.
Document Page
Blog: Selection Sort (Database
programming)
1
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
Selection Sort (Database programming)
6th August 2019
Selection Sort Algorithm
The selection sort algorithm can be referred to as the process through which values are sorted.
For sorting a data in the ascending order, a comparison is undertaken between the 0th element and
the other elements. If it is found that the 0th element is greater in value than the element it is
being compared with, there is an interchange between the two values. What happens here is that,
after the first iteration occurs, the element with the smallest value is placed at the 0th position.
The process is undertaken continuously until there is sorting out of the entire array. Selection sort
can be defined as the simplest sorting algorithm. It first finds out the smallest element within the
array and then it swaps that element with the one in the first position. After that, it searches for
the second smallest element and will swap it with the element in the second position. This
process will be continued till the whole array has been sorted. The figure that is represented
below depicts the technique in which the selection sort takes place.
2
Document Page
An example of the process through which selection sort is done in programming is as follows:
#include <iostream>
using namespace std;
int main()
{
int a[100], i, n, p, k, min, loc, tmp;
cout << "\n------------ SELECTION SORT ------------ \n\n";
3
Document Page
cout << "Enter No. of Elements:"; cin >> n;
cout << "\nEnter Elements:\n";
for (i = 1; i <= n; i++) { cin >> a[i];
}
for (p = 1; p <= n - 1; p++) // Loop for Pass
{
min = a[p]; // Element Selection
loc = p;
for (k = p + 1; k <= n; k++) // Finding Min Value { if (min > a[k]) {
min = a[k];
loc = k;
}
}
tmp = a[p];
a[p] = a[loc];
a[loc] = tmp;
}
cout << "\nAfter Sorting: \n";
for (i = 1; i <= n; i++) {
cout << a[i] << endl;
4
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
}
}
Selection sort algorithm is also defined as the process which helps in sorting an array by
adopting the process where repeatedly the minimum element is found out in an ascending order.
The sorting is done from the unsorted part and it is put in the beginning. The algorithm ensures
that there are two sub-arrays within a given array. It consists of the sub-array that has already
been sorted and the sub-array which is remaining and is unsorted. In each of the iterations of
selection sort, the minimum element that derived through ascending order from the sub-array that
is unsorted is selected and shifted to the sub-array that is sorted. The steps that have been
discussed above can be more effectively depicted with the help of the following example.
5
Document Page
A Flowchart of Selection Sort
6
arr[] = 64 25 12 22 11
// Find the minimum element in arr[0...4]
// and place it at beginning
11 25 12 22 64
// Find the minimum element in arr[1...4]
// and place it at beginning of arr[1...4]
11 12 25 22 64
// Find the minimum element in arr[2...4]
// and place it at beginning of arr[2...4]
11 12 22 25 64
// Find the minimum element in arr[3...4]
// and place it at beginning of arr[3...4]
11 12 22 25 64
Document Page
7
chevron_up_icon
1 out of 7
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]