﻿ C# example- selection sort algorithm

C# example- selection sort algorithm

Selection sort algorithm

This is C# example code for a selection sort algorithm. In the selection sort, the list to be sorted is divided into tow parts: sorted and unsorted parts. The smallest item in the unsorted part is swapped with the beginning item of the unsorted list. Then the number of items in the sorted list increases by one and the number of the unsorted list decreases by one. To get more information about selection sort, i recommend you to read:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
int[] arr= new int[5]{23,2,3,34,6}; //unsorted data set
selectsort(arr,5); //sorting process using selection sort
int i;
for(i=0;i<5;i++)
Console.Write(arr[i]+"\t"); //after sorting
Console.ReadLine();

}

///selection sort
static void selectsort(int[] dataset, int n){
int i,j;
for(i=0;i<n;i++){
int min=i;
for(j=i+1;j<n;j++)
if(dataset[j]<dataset[min]) min=j; //find min value
//then swap it with the beginning item of the unsorted list
int temp=dataset[i];
dataset[i]=dataset[min];
dataset[min]=temp;

}

}

}
}

Comments

 PaulThanks. Its easy to copy and paste into C# 2018-03-11 The Garbage Collectoryou will all be nulled by the garbage collector2017-02-20 sdsdssdsd2016-03-29 Oviagay bastardo 2016-02-16 JohanPlease wrap the code in a code block or something? This is awful to read!2015-09-05 viswaI couldnt able to see or understand clearly because of font sizes.Please change it .And post some detailed programs.2015-08-19 viswaI couldnt able to see or understand clearly because of font sizes.Please change it 2015-08-19

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.