I've tried to make a bubble sort algorithm but when I output the values sorted in order it gives me an extra value at the beginning and this value isn't in my array but I can't work out how to fix this.
For example when I run the program and my array is: 8, 3, 68, 74, 67, 82, 82, 18, 48, 53
The sorted values show: 60, 3, 8, 18, 48, 53, 67, 68, 74,82, 82
public static void Main(string[] args)
{
int n = 10; //10 values in array
Random r = new Random();
int[] a; //array
int temp;
int i;
a = new int[n + 1];
a[0] = 1; //starts at 0
for (i = 0; i <= n; i++) // set the array up
a[i] = r.Next(1, 100); // + random numbers
for (i = 1; i <= n; i++)
Console.WriteLine(a[i] + " "); // outputs the numbers of array
Console.WriteLine();
Console.ReadLine();
for (i = 1; i <= n; i++)
{
for (int k = 1; k < a.Length - 1; k++) // n - 1 passes
{
if (a[k] > a[k + 1])
{
temp = a[k + 1]; //stores temporarily
a[k + 1] = a[k];
a[k] = temp;
}
}
}
Console.WriteLine("Array is sorted: ");
foreach (int number in a) Console.Write(number + " ");
Console.Read();
}
Here's what's happening:
You're initializing your array with 11
elements when you do this:
int n = 10; int[] a = new int[n + 1]; // n + 1 is 11, so this creates an 11-element array
Then when you populate the array, you loop from 0
to 10
, populating all 11 indexes:
for (i = 0; i <= n; i++) a[i] = r.Next(1, 100);`
When you sort the array, however, you ignore the first element (at index 0
) because your loops always start with 1
, for example:
for (i = 1; i <= n; i++) { for (int k = 1; k < a.Length - 1; k++) { // Sort the elements here } }
But then at the very end, you output ALL the elements, so you get to see the first number that you were skipping during the sort:
foreach (int number in a) Console.Write(number + " ");
To fix this, normally when looping through an array we start at index 0
and we loop while our index variable is less than the array length:
for (i = 0; i < a.Length; i++)
This will always ensure you iterate over each item in the array.
I would consider a more Linq-supported approach:
using System;
using System.Collections.Generic;
using System.Linq;
namespace BubbleSort
{
class Program
{
static readonly Random rnd = new Random();
static void Swap(IList<int> list, int indexA, int indexB)
{
int tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
}
static void Main(string[] args)
{
var array = Enumerable.Range(1, 10).Select(r => rnd.Next(1,100)).ToArray();
Console.WriteLine("Unsorted array:" + String.Join(" ", array.Select(x => x.ToString())));
// array.Sort(); // probably not allowed
for (int i = 0; i < array.Count() - 1; ++i)
{
for (int j = 0; j < array.Count() - 1; ++j)
{
if (array[j] > array[j + 1]) Swap(array, j, j + 1);
}
}
Console.WriteLine("Sorted array:" + String.Join(" ", array.Select(x => x.ToString())));
}
}
}
Note that your Bubble sort algo is in it's most inefficient form, always repeating n-1
times. Check Wikipedia for some tips on optimizing. Eg
var swapped = true;
while (swapped)
{
swapped = false;
for (int i = 0; i < array.Count() - 1; ++i)
{
if (array[i] > array[i + 1])
{
Swap(array, i, i + 1);
swapped = true;
}
}
}
Make all your loops:
for(int SOMETHING = 0; ...
Not
for(int SOMETHING = 1; ...
OR
Make your final attempt at printing out the array like the earlier attempt (use an index starting from 1 not a foreach)
Arrays in c# start from 0. Per my comments; you were filling and sorting your arrays starting from index 1, but the final time that you print your array out you use a foreach which will include the 0th (first) element, which you didnt print, or sort, in the earlier parts of the algorithm
Here's a fixed up version of your code:
public static void Main(string[] args)
{
int n = 10; //10 values in array
Random r = new Random();
int[] a = new int[n]; //array
for (int i = 0; i < a.Length; i++) // set the array up
a[i] = r.Next(1, 100); // + random numbers
foreach(int x in a)
Console.WriteLine(x + " "); // outputs the numbers of array
Console.WriteLine();
Console.ReadLine();
for (int i = 0; i < a.Length - 1; i++)
{
for (int j = 0; j < a.Length - 1; j++)
{
if (a[j] > a[j + 1])
{
int temp = a[j + 1]; //stores temporarily
a[j + 1] = a[j];
a[j] = temp;
}
}
}
Console.WriteLine("Array is sorted: ");
foreach (int number in a)
Console.Write(number + " ");
Console.Read();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.