Array Sorting Program in C#(Sharp)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ArraySorting
{
class Program
{
static void Main(string[] args)
{
/* sorting the Array
* we are creating the 3 array. arr,asc,desc.
* arr for storing the array.
* asc for storing the even numbers.
* desc for storing the odd numbers.
* using for loop for accepting the array values,and print the values.
* we are using the Array.Resize() for creating the asc(array) size in run time.
* usig Array.Sort() for sorting the array.
* using Array.Reverse() for reversing the sorting array and sorting the array in descending order.
*/
int[] array = new int[10];
int[] asc = new int[0];
int[] dsc = new int[0];
int j = 0, k = 0;
for (int i = 0; i < 10; i++)
{
Console.Write("Enter the number\t");
array[i] = Convert.ToInt32(Console.ReadLine());
if (array[i] % 2 == 0)
{
Array.Resize(ref asc, asc.Length + 1);
asc[asc.Length-1] = asc.Length + 1;
asc[j] = array[i];
j = j + 1;
}
else
{
Array.Resize(ref dsc, dsc.Length + 1);
dsc[dsc.Length - 1] = dsc.Length + 1;
dsc[k] = array[i];
k = k + 1;
}
}
Array.Sort(asc);
Array.Sort(dsc);
Array.Reverse(dsc);
int a =asc.Count();
int b =dsc.Count();
for (j = 0; j!=(asc.Length) ; j++)
Console.WriteLine(asc[j]);
for (k = 0; k!=(dsc.Length); k++)
Console.WriteLine(dsc[k]);
Console.ReadLine();
}
}
}

No comments:
Post a Comment