When I run my program the first set of 20 numbers print on my console with one extra number (the last number of the sorted array).
The second set of numbers which is the last 20 numbers of the sorted array prints correctly. What am I doing wrong.
Thank you for your help.
namespace Sorting
{
class Program
{
static void Main(string[] args)
{
int count;
int[] value = new int[1000];
GetValues(value);
CombSortValues(value);
for (int i = 0; i < 20; i++)
Console.Write("{0}, ", value[i]);
Console.WriteLine(" " + value[value.Length - 1]);
for (int i = 980; i < value.Length - 1; i++)
Console.Write("{0}, ", value[i]);
Console.WriteLine("" + value[value.Length - 1]);
Console.ReadKey();
}
static void GetValues(int[] a)
{
Random randomNumber = new Random();
for (int i = 0; i < a.Length; i++)
a[i] = randomNumber.Next(100, 999);
}
public static int[] CombSortValues(int[] a)
{
int gap = a.Length;
double shrink = 1.3;
while (true)
{
if (gap > 1)
{
gap = (int)Convert.ToDouble(gap / shrink);
if (gap == 9 || gap == 10)
{
gap = 11;
}
}
bool isSwapped = false;
for (int i = 0; i + gap < a.Length; ++i)
{
if (a[i] > a[i + gap])
{
int swapValue = a[i];
a[i] = a[i + gap];
a[i + gap] = swapValue;
isSwapped = true;
}
}
if (gap == 1 && !isSwapped)
{
break;
}
}
return a;

New Topic/Question
Reply




MultiQuote




|