2
Answers

C# Selection Sort example need to be explained

Nony reay

Nony reay

16y
3.9k
1

I face some problems to understand sort example so that i am just wondering if some one give me a hand and explain it to me
Thank you for your assistant
+++++++++++++++++++++++++++++++++++

 

using System;
class TestSelectionSort;
{
  public static void Main( )
  {
    int[] sortArray = {7, 3, 66, 3, -5, 22, -77, 2};
    SelectionSort(sortArray);
    foreach (int element in sortArray)
      Console.Write(element + "\t");
    Console.WriteLine();
  }
// Include remaining code here
} // end TestSelectionSort

 

 

// sort using the selection sort algorithm
 static void SelectionSort(int[] data)
 {
    int next, indexOfMin;
    for (next=0; next<data.Length - 1; next++)
    {
      indexOfMin = Min(data,next,data.Length-1);
      Swap(data, indexOfMin, next);
    }
 }
 
 
 
 
// find the smallest element in a specified range
  static int Min(int[] data, int start, int end)
  {
    int minIndex = start;
    for (int i = start + 1; i <= end; ++i)
      if (data[i] < data[minIndex])
        minIndex = i;  // found a smaller value
    return minIndex;
  }
 
 
 
static void Swap(int[] data, int first, int second)
  {
    int temp;
    temp = data[first];
    data[first] = data[second];
    data[second] = temp;
  }
Answers (2)