Write a program to find the minimum number of steps to sort

Niladri.Biswas
Posted by Niladri.Biswas under C# category on | Points: 40 | Views : 848
public static int get_order(int[] input1)
{
var sortedArray = input1.OrderByDescending(o => o).ToArray();
var unsortedArray = input1;
int temp1;
int swap = 0;

int arrayLength = sortedArray.Length;
for (int i = 0; i < arrayLength; i++)
{
if (sortedArray[i] != unsortedArray[i])
{
temp1 = unsortedArray[i];
unsortedArray[i] = sortedArray[i];
for (int j = i + 1; j < arrayLength; j++)
{
if (unsortedArray[j] == sortedArray[i])
{
unsortedArray[j] = temp1;
swap++;
break;
}
}
}
}

return swap;
}


Invoke it as

static void Main(string[] args)
{
int[] number = { 3,1,2};

int result = get_order(number);

Console.ReadKey();
}

Comments or Responses

Login to post response