Monday, January 20, 2014

Array Sorting program.


static void Main(string[] args)
{
int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9, 3, 4, 5, 6, 6, 6, 6, 6, 6, 6, 3, 32, 4, 6, 6, 6 };
int temp = 0;
for (int write = 0; write < arr.Length; write++)
{
for (int sort = 0; sort < arr.Length - 1; sort++)
{
if (arr[sort] > arr[sort + 1])
{
temp = arr[sort + 1];
arr[sort + 1] = arr[sort];
arr[sort] = temp;
}
}
}

for (int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " ");
//--------------------
Array.Sort(arr);
for (int i = 0; i < arr.Length; i++) Console.Write(arr[i] + " ");

//-------------------
int[] arr1 = { 1, 5, 4, 8, 2,999,5 };
int temp1 = 0;
for (int i = 0; i <= arr1.Length - 1; i++)
{
for (int j = 0; j <= i - 1; j++)
{
if (arr1[i] < arr1[j])
{
temp1 = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp1;
temp1 = 0;
}
}
}
for (int i = 0; i < arr1.Length; i++) Console.Write(arr1[i] + " ");
Console.ReadKey();
Console.Read();
}
}

No comments:

Post a Comment