Selection Sort
Written by: maffelu , 2009-09-16 15:55:32
| Array | Lowest |
|---|---|
| 3, 2, 5, 1, 4 | 2 |
| 3, 2, 5, 1, 4 | 2 |
| 3, 2, 5, 1, 4 | 1 |
| 3, 2, 5, 1, 4 | 1 |
| Array | Lowest |
|---|---|
| 1, 2, 5, 3, 4 | 5 |
| 1, 2, 5, 3, 4 | 3 |
| 1, 2, 5, 3, 4 | 3 |
| Array | Lowest |
|---|---|
| 1, 2, 5, 3, 4 | 3 |
| 1, 2, 5, 3, 4 | 3 |
| Array | Lowest |
|---|---|
| 1, 2, 3, 5, 4 | 4 |
Void SelectionSort(Array arr){
For (var outer = 0; outer < n; outer++){
minimum value = outer;
For(var inner = outer + 1; inner < n + 1; inner++){
If(inner value < minimum value)
minValue = inner;
}
If(outer value is not minimum value){
var temp = outer value;
outer value = minimum value;
minimum value = temp;
}
}
}
public void SelectionSort(int[] arr) {
//Outer loop
for (int outer = 0; outer < (arr.Length - 1); outer++) {
//Holds the current minimum value in the array
int min = outer;
//Inner loop
for (int inner = outer + 1; inner < arr.Length; inner++) {
//If the current value to check is lower than
//the current minimum value, set minimum value
//to the index this value
if (arr[inner] < arr[min])
min = inner;
}
//Swap
if (outer != min) {
int temp = arr[outer];
arr[outer] = arr[min];
arr[min] = temp;
}
}
}
There are 6 comments on this article.
sahil raina
2011-10-19 12:16:09
the explanation is good.can i have the code for all sorting and searching techniques in c++.it will b g8 of you.
thank you
Anu
2011-11-14 04:19:34
I understood the concept by looking at this example, i was really confused about how it finds min element. thank you very much
uthappa c.n
2011-11-20 17:55:52
its really nice.....
amir
2012-01-29 22:04:44
more helpful for the presentation in our class thanks of almighty allah
HAIDER ALI
2012-01-30 14:17:53
very very briefly explained
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
Azeem
2011-05-13 21:13:26
its really helpful for me.......