Sort the data - Selection sort algorithm

"Selection sort" program is designed to sort the data by using the Algorithm with the same name.

If you read the description of this type of algorithm to sort the data, then an analysis of the supplied code below will not be a problem.

Attached is the Selection sort.zip archive with a complete solution SharpDevelop IDE written in C#, which you can download and within which is located in the Release folder, exe file of the program. For proper work of program it is necessary to have .Net framework 4.0 ( ^ ) installed.

Program code :

/*

* Created by SharpDevelop.

* User: Perić Željko

* Date: 04.02.2012

* Time: 12:47

*

* A simple program to sort the numbers from largest to smallest with the help

* of so-called "Selection sort" algorithm. The easiest to understand but it is

* the slowest of all the algorithms when it comes to time spent for sorting.

* Made for sorting numbers, in descending order, from largest to smallest.

* The number of members of the array is limited to 5, and the value of the

* members must be from the set of real numbers. Uses a comparison of the value

* of the first element in a row with the value of other, and if some of them

* are larger replaces them, so that the maximum value after the first pass

* through the whole series, comes in first place in the series. This operation

* is repeated, except that it starts from the second, third and fourth to the

* last member of the series. So the next highest value in the series is placed

* to the second, third, fourth and fifth place in the series.

* Very quickly you can change the program and get a sorted array in ascending order

*

* instead if (Niz[Brojač] < Niz[X]) write if (Niz[Brojač] > Niz[X])

*

* or when printing a series

*

* Brojač = 6;

* X = 0;

* while (Brojač != 1)

* {

* Brojač = Brojač - 1;

* X = X + 1;

* Console.WriteLine(" " + X.ToString() + ". member is : " + Niz[Brojač].ToString());

* }

*

*/

using System;

namespace Selection_sort

{

class Program

{

public static void Main(string[] args)

{

//

// Variable declaration

//

double [] Niz = new double[10];

double Najveći = 0;

double Element = 0;

bool Realan = false;

int Brojač = 0;

int X = 0;

Console.Title = " Selection sort ";

Console.SetWindowSize(80,40);

Console.ForegroundColor = ConsoleColor.Green;

Console.Clear();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

Console.WriteLine(" Program for array sorting with 'Selection sort' algorithm");

Console.WriteLine();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

while (Brojač != 5)

{

Brojač = Brojač + 1;

Console.Write(" Enter value of " + Brojač + ". memeber : ");

Realan = double.TryParse(Console.ReadLine(),out Element);

while (Realan == false)

{

Console.WriteLine("Warning ! Entry error.");

Console.Write(" Enter value of " + Brojač + ". memeber : ");

Realan = double.TryParse(Console.ReadLine(),out Element);

}

Niz[Brojač] = Element;

}

Brojač = 0;

while (Brojač != 4)

{

Brojač = Brojač + 1;

X = Brojač;

while (X != 5)

{

X = X + 1;

if (Niz[Brojač] < Niz[X])

{

Najveći = Niz[X];

Niz[X] = Niz[Brojač];

Niz[Brojač] = Najveći;

}

}

}

Console.WriteLine();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

Console.WriteLine(" Sorted in descending order");

Console.WriteLine();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

Brojač = 0;

while (Brojač != 5)

{

Brojač = Brojač + 1;

Console.WriteLine(" " + Brojač.ToString() + ". member is : " + Niz[Brojač].ToString());

}

Console.WriteLine();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

Console.WriteLine(" Sorted in ascending order");

Console.WriteLine();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

Brojač = 6;

X = 0;

while (Brojač != 1)

{

Brojač = Brojač - 1;

X = X + 1;

Console.WriteLine(" " + X.ToString() + ". member is : " + Niz[Brojač].ToString());

}

Console.WriteLine();

Console.WriteLine("*******************************************************************");

Console.WriteLine();

Console.Write("Press any key to continue . . . ");

Console.ReadKey(true);

}

}

}

Attached is the Selection sort.zip archive with a complete solution SharpDevelop IDE written in C#,

which you can download and within which there is a Release folder with exe file of the program.

For proper work of program it is necessary to have .Net framework 4.0 ( ^ ) installed.

All the best,

Author