# How to sort the numbers using c#

I need some help in code to be able to handle the following logic.

The program gets many input in Integers like 10,16,3,17,21,29,6

Logic to be done :

scenario : 1

First select the biggest 4 numbers of input from array which is 16,17,21,29
Now assign the values to A,B,C and D

A = smallest in the selected 4
B = biggest in the selected 4
C = second smallest in the selected 4
D = third smallest in the selected 4

Result to be Displayed :

A = 16
B = 29
C = 17
D = 21

scenario : 2

If the user gives 3 inputs like 3,6,10
assign only to A,B,C and shud ignore D

Result to be Displayed :

A = 3
B = 10
C = 6

Asked:
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
@lucky_james, pls provide with some sample code for this req. Thanks.
Commented:
you can implement the sorting algorithm as shown/given in the codeproject.
Post sorting, all you have to do is to access that location of the collection.
A = smallest in the selected 4 = coll[coll.Count-1]
B = biggest in the selected 4 = coll[0]
C = second smallest in the selected 4 = coll[ coll.Count-1]
D = third smallest in the selected 4 = if(coll.Count>3) coll[coll.Count-2]

Hope this helps.
Commented:
Alternatively, if you think that you will not need any variable "E" which should contain (n)th biggest/smallest number,  you can design you code as follows:

A = B = C = D = -1;
iterate thru the collection of numbers:
if A == -1, A = X;
else if (A > X) {D = C; C=A; A = X; }
if B == -1, B = X;
else if B < X B = X;

Commented:
If you are using list and dont want to implement any such algorithm on your own, you can use the Sort method provided by .net.
check out:
http://www.developerfusion.com/code/5513/sorting-and-searching-using-c-lists/
Above link gives the sample code as well as explains the usage using Generic List.

let me know if you are stuck somewhere.

James

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static int[] Arrays;

static void Main(string[] args)
{
int A = 0;
int B = 0;
int C = 0;
int D = 0;
Arrays = new int[7] { 10, 16, 3, 17, 21, 29, 6 };

bubble_sort(Arrays);

for (int i = 0; i < 4; i++)
{
System.Console.WriteLine(Arrays[i]);
}
if (!A.Equals("0"))
{
A = Arrays[3];
System.Console.WriteLine("A:" + A);
}
if (!B.Equals("0"))
{
B = Arrays[2];
System.Console.WriteLine("B:" + B);
}
if (!C.Equals("0"))
{
C = Arrays[1];
System.Console.WriteLine("C:" + C);
}
if (!D.Equals("0"))
{
D = Arrays[0];
System.Console.WriteLine("D:" + D);
}

}

static void bubble_sort(int[] dizi)

{

for (int i = 0; i < dizi.Length - 1; i++)

{

for (int j = 1; j < dizi.Length - i; j++)

{

if (dizi[j] > dizi[j - 1])

{

int gecici = dizi[j - 1];

dizi[j - 1] = dizi[j];

dizi[j] = gecici;

}

}

}

}

}
}
Commented:

``````class Program
{
static void Main(string[] args)
{
const int MinNumbersCount = 3;
const int BiggestNumbersCount = 4;
List<int> numbers = new List<int>();

int number;
while (Int32.TryParse(Console.ReadLine(), out number))
numbers.Add(number);

if (numbers.Count < MinNumbersCount)
{
Console.WriteLine("You should input at least {0} number(s)", MinNumbersCount);
return;
}

numbers.Sort();
List<int> biggestNumbers = numbers.GetRange(Math.Max(0, numbers.Count - BiggestNumbersCount), Math.Min(BiggestNumbersCount, numbers.Count));

Console.WriteLine("A = {0}", biggestNumbers[0]);
Console.WriteLine("B = {0}", biggestNumbers[biggestNumbers.Count - 1]);
Console.WriteLine("C = {0}", biggestNumbers[1]);
if (biggestNumbers.Count > MinNumbersCount)
Console.WriteLine("D = {0}", biggestNumbers[biggestNumbers.Count - 2]);
// Say hello to your teacher!
}
}
``````
Commented:
Object-oriented way:

``````class Program
{
static void Main(string[] args)
{
Homework homework = new Homework(4, 3);
// For tests use:
// homework.AddRangeOfNumbers(new int[] { 10, 16, 3, 17, 21, 29, 6 });
// homework.AddRangeOfNumbers(new int[] { 3,6,10 });

Console.WriteLine("Enter number, or any other value to stop entering:");
int number;
while (Int32.TryParse(Console.ReadLine(), out number))
homework.AddNumber(number);

if (!homework.IsEnoughNumbers)
{
Console.WriteLine("You should input at least {0} number(s)", homework.MinNumbersCount);
return;
}

Console.WriteLine("A = {0}", homework.A);
Console.WriteLine("B = {0}", homework.B);
Console.WriteLine("C = {0}", homework.C);
if (homework.CanGetD)
Console.WriteLine("D = {0}", homework.D);
// Say hello to your teacher!
}
}

class Homework
{
private List<int> _numbers = new List<int>();
private readonly int _biggestNumbersCount;
private readonly int _minNumbersCount;
private bool _isNumbersSorted;

public Homework(int biggestNumbersCount, int minNumbersCount)
{
// Add contract by yourself.
_biggestNumbersCount = biggestNumbersCount;
_minNumbersCount = minNumbersCount;
}

public void AddNumber(int number)
{
_numbers.Add(number);
_isNumbersSorted = false;
}

public void AddRangeOfNumbers(IEnumerable<int> collection)
{
_numbers.AddRange(collection);
_isNumbersSorted = false;
}

public bool IsEnoughNumbers
{
get { return _numbers.Count >= _minNumbersCount; }
}

public int MinNumbersCount
{
get { return _minNumbersCount; }
}

public bool CanGetD
{
get { return _numbers.Count > _minNumbersCount; }
}

private List<int> BiggestNumbers
{
get
{
if (!_isNumbersSorted)
{
_numbers.Sort();
_isNumbersSorted = true;
}

int startIndex = Math.Max(0, _numbers.Count - _biggestNumbersCount);
int count = Math.Min(_biggestNumbersCount, _numbers.Count);
return _numbers.GetRange(startIndex, count);
}
}

/// <summary>
/// Smallest in the selected.
/// </summary>
public int A
{
get { return BiggestNumbers[0]; }
}

/// <summary>
/// Biggest in the selected.
/// </summary>
public int B
{
get { return BiggestNumbers[BiggestNumbers.Count - 1]; }
}

/// <summary>
/// Second smallest in the selected.
/// </summary>
public int C
{
get { return BiggestNumbers[1]; }
}

/// <summary>
/// Third smallest in the selected.
/// </summary>
public int D
{
get { return BiggestNumbers[2]; }
}
}
``````
Commented:
If you have your numbers in an array, you can just use the Sort method of the Array class.

Note that almost half of this code just displays the result of applying your algorithm
``````        private void test12()
{
int[] a = { 10, 16, 3, 17, 21, 29, 6 };
//int[] a = { 3,6,10 };
int A, B, C, D;
string s = "";
Array.Sort(a);
int k = a.Length;
if (k > 3)
{
A = a[k - 4];
B = a[k - 1];
C = a[k - 3];
D = a[k - 2];
s += "A = " + A.ToString();
s += "\nB = " + B.ToString();
s += "\nB = " + C.ToString();
s += "\nB = " + D.ToString();
}
else if (k > 2)
{
A = a[k - 3];
B = a[k - 1];
C = a[k - 2];
s += "A = " + A.ToString();
s += "\nB = " + B.ToString();
s += "\nB = " + C.ToString();
}
else if (k > 1)
{
A = a[k - 2];
B = a[k - 1];
s += "A = " + A.ToString();
s += "\nB = " + B.ToString();
}
else if (k > 0)
{
A = a[k - 1];
s += "A = " + A.ToString();
}
else
{
s = "No data";
}
MessageBox.Show(s);
}
``````
Commented:
I'm impressed :)
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.