I want to sort an array[10] with bubblesort and place the unsorted/sorted items in an listbox.
But the array isn't sorted and gives me always the same array of numbers back.
private void button1_Click(object sender, EventArgs e)
{
lblB.Show();
lblQ.Hide();
lbS.Items.Clear();
lbU.Items.Clear();
Random r = new Random();
int n = 10;
int[] arr = new int[n];
//listbox fill random
for (int i = 0; i < arr.Length; i++)
{
arr[i] = r.Next(0, 20);
lbU.Items.Add(arr[i]);
}
int temp = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i] < arr.Length)
{
temp = arr[i];
arr[i] = j;
arr[j] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
lbS.Items.Add(arr[i]);
}
}
Am i missing something here,
Any help would be greatly appreciated.
You have a mistake in your code that's sorting the array:
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
if (arr[i] < arr.Length)
{
temp = arr[i];
arr[i] = j; // <<-- Mistake here - should be arr[i] = arr[j];
arr[j] = temp;
}
}
}
Related
I've been working on a sorting algorithm and one condition says that the elements in the array should be program generated.
This is the code I've been working on.
int f;
Console.WriteLine("Enter how many elements you want to be sorted:");
f = Convert.ToInt32(Console.ReadLine());
int[] myArray = new int[f];
int smallest, tmp;
Console.WriteLine("Unsorted List");
foreach (int a in myArray)
Console.Write(a + " ");
for (int i = 0; i < myArray.Length - 1; i++)
{
smallest = i;
for (int j = i + 1; j < myArray.Length; j++)
{
if (myArray[j] < myArray[smallest])
{
smallest = j;
}
tmp = myArray[smallest];
myArray[smallest] = myArray[i];
myArray[i] = tmp;
}
}
Console.WriteLine("\nSorted List Using Selection Sort:");
for (int i=0; i < myArray.Length; i++)
{
Console.Write(myArray[i] + " ");
The result of this program is just 0. How can I make the elements in array program generated? Is there a specific code block?
Please note we should use i < myArray.Length to Iterate the array instead of i < myArray.Length-1.
You can try the following code to add the elements in program generated array.
static void Main(string[] args)
{
int f;
Console.WriteLine("Enter how many elements you want to be sorted:");
f = Convert.ToInt32(Console.ReadLine());
int[] myArray = new int[f];
int temp = 0;
int minIndex = 0;
Console.WriteLine("Unsorted List");
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = Convert.ToInt32(Console.ReadLine());
}
for (int i = 0; i < myArray.Length; i++)
{
minIndex = i;
for (int j = i; j < myArray.Length; j++)
{
if (myArray[j] < myArray[minIndex])
{
minIndex = j;
}
}
temp = myArray[minIndex];
myArray[minIndex] = myArray[i];
myArray[i] = temp;
}
Console.WriteLine("\nSorted List Using Selection Sort:");
for (int i = 0; i < myArray.Length; i++)
{
Console.Write(myArray[i] + " ");
}
Console.ReadKey();
}
Besides, I have modified the related code about Selection Sort, which makes it can produce the correct sort.
Result:
Update for generate the random array:
int f;
Console.WriteLine("Enter how many elements you want to be sorted:");
f = Convert.ToInt32(Console.ReadLine());
int[] myArray = new int[f];
Random randNum = new Random();
for (int i = 0; i < myArray.Length; i++)
{
myArray[i] = randNum.Next(1, 100);
}
Console.WriteLine("The radnom array is ");
foreach (var item in myArray)
{
Console.WriteLine(item);
}
The following code sorts rows by the first element using bubble method.
I can't change it to counting sort.
public void SortStack(double[,] n)
{
for (int i = 0; i < n.GetLength(0) - 1; i++)
{
for (int j = i; j < n.GetLength(0); j++)
{
if (n[i, 0] > n[j, 0])
{
for (int k = 0; k < n.GetLength(1); k++)
{
var temp = n[i, k];
n[i, k] = n[j, k];
n[j, k] = temp;
}
}
}
}
}
Please help.
As you do bubble sort based on first element of each row. you should do counting sort like that too. so you just need to count first item of each row.
private static int[,] CountingSort2D(int[,] arr)
{
// find the max number by first item of each row
int max = arr[0, 0];
for (int i = 0; i < arr.GetLength(0); i++)
{
if (arr[i, 0] > max) max = arr[i, 0];
}
// we want to get count of first items of each row. thus we need 1d array.
int[] counts = new int[max + 1];
// do the counting. again on first index of each row
for (int i = 0; i < arr.GetLength(0); i++)
{
counts[arr[i, 0]]++;
}
for (int i = 1; i < counts.Length; i++)
{
counts[i] += counts[i - 1];
}
// result is sorted array
int[,] result = new int[arr.GetLength(0), arr.GetLength(1)];
for (int i = arr.GetLength(0) - 1; i >= 0; i--)
{
counts[arr[i, 0]]--;
// now we need to copy columns too. thus we need another loop
for (int j = 0; j < arr.GetLength(1); j++)
{
result[counts[arr[i, 0]], j] = arr[i, j];
}
}
return result;
}
Here is the test.
static void Main()
{
Random rand = new Random();
int[,] arr = new int[3,3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
arr[i, j] = rand.Next(0, 100);
}
}
int[,] newarr = CountingSort2D(arr);
for (int i = 0; i < arr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < arr.GetLength(1); j++)
{
Console.Write(arr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
for (int i = 0; i < newarr.GetLength(0); i++)
{
Console.Write("{ ");
for (int j = 0; j < newarr.GetLength(1); j++)
{
Console.Write(newarr[i, j] + " ,");
}
Console.Write("} ,");
}
Console.WriteLine();
}
Example Output:
{ 49,66,8 },{ 33,39,64 },{ 65,52,76 } // Original array
{ 33,39,64 },{ 49,66,8 },{ 65,52,76 } // Sorted array
enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testing_random
{
class Program
{
static void Main(string[] args)
{
int n = 4;
int[,] a = new int[n,n];//declaring the matrix
Random o = new Random();
a[0,0] = o.Next(n);
for (int i = 1; i < n; i++)//filling the first line
{
int d = 1;
while (d != 0)
{
a[i,0] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i,0] == a[j,0])
d++;
}
}
for (int i = 1; i < n; i++)//filing the first column
{
int d = 1;
while (d != 0)
{
a[0, i] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[0, i] == a[0, j])
d++;
}
}
for (int k = 1; k < n; k++)//filling the rest of the matrix
for (int i = 1; i < n; i++)
{
int d = 1;
while (d != 0)
{
a[i, k] = o.Next(n);
d = 0;
for (int j = 0; j < i; j++)
if (a[i, k] == a[j, k])
d++;
for (int j = 0; j < k; j++)
if (a[i, k] == a[i, j])
d++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
Console.Write("{0} ", a[i, j]);
Console.WriteLine();
}
Console.ReadLine();
}
}
}
The output should be a matrix of 4*4 where each column and each line contains each number once.
The problem is when i run the code, not every time i get an output, i think the problem is not every set of first line and column can give a matrix as required the i get into an unending loop.
what i want to do is limit the running time of the application to 100 ms per example,so if the matrix is not filled,the program restarts
What piece of code am i missing?
replace the while( d != 0 ) with a loop which counts up to a certain very large maximum number of iterations. (Try 1000, 100000, whatever.)
Are you trying to randomly insert numbers between 1-4 in the first line of the array? If so there is a much easier way to do it.
You can generate the 4 numbers to be inserted into the array and then just look through the first line of the array and set each value.
Random rnd = new Random();
var randomNumbers = Enumerable.Range(1, 4).OrderBy(i => rnd.Next()).ToArray();
for (int i = 0; i < n; i++)
{
a[i, 0] = randomNumbers[i];
}
so im having this homework assignment (the only reason why one would do bubblesort )
Using visual studio i made 2 arrays one in witch the unsorted numbers are and a second where the numbers are sorted.
My code as i will paste it here, will just return 0's in both arrays and i have no idea what im doing wrong so please help me out !
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBubbleSort_Click(object sender, EventArgs e)
{
int[] numbers = new int[100];
foreach (int value in numbers)
{
lstBubbleUnorderd.Items.Add(value);
}
for (int y = 0; y < 100; y++)
{
for (int i = 100 - 1; i > y; --i)
{
if (numbers[i] <= numbers[i - 1])
{
int temp = numbers[i];
numbers[i] = numbers[i - 1];
numbers[i - 1] = temp;
}
}
}
for (int j = 0; j < 100; j++)
{
lstBubbleOrderd.Items.Add(numbers[j]);
}
}
}
}
===========================================================================================
My code is now working thanks to you guy's
My new code is as follows :
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBubbleSort_Click(object sender, EventArgs e)
{
lstBubbleOrderd.Items.Clear();
lstBubbleUnorderd.Items.Clear();
Random RndNmbrs = new Random();
int n = RndNmbrs.Next();
for (int a = 0; a < 10; a++)
lstBubbleUnorderd.Items.Add(RndNmbrs.Next(0, 10));
int[] numbers = new int[lstBubbleUnorderd.Items.Count];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = int.Parse(lstBubbleUnorderd.Items[i].ToString());
}
for (int y = 0; y < numbers.Length; y++)
{
for (int i = numbers.Length - 1; i > y; --i)
{
if (numbers[i] <= numbers[i - 1])
{
int temp = numbers[i];
numbers[i] = numbers[i - 1];
numbers[i - 1] = temp;
}
}
for (int j = 0; j < numbers.Length; j++)
{
lstBubbleOrderd.Items.Add(numbers[j]);
}
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
You created your array of numbers here:
int[] numbers = new int[100];
But you never populated it with anything, so all of its values are initialized to zero.
Perhaps what you meant to do was something like this (incorporating suggestions from leemo's answer)?
int[] numbers = new int[lstBubbleUnorderd.Items.Count];
for (int i = 0; i < numbers.Length; i++)
{
numbers[i] = int.Parse(lstBubbleUnorderd.Items[i].ToString());
}
for (int y = 0; y < numbers.Length; y++)
{
for (int i = numbers.Length - 1; i > y; --i)
{
if (numbers[i] <= numbers[i - 1])
{
int temp = numbers[i];
numbers[i] = numbers[i - 1];
numbers[i - 1] = temp;
}
}
}
for (int j = 0; j < numbers.Length; j++)
{
lstBubbleOrderd.Items.Add(numbers[j]);
}
This will read any numbers that were entered into lstBubbleUnorderd into your numbers array, sort them, then print them out in lstBubbleOrderd.
In addition to the answer from #p.s.w.g :
You may want to consider avoiding literal count thresholds in your loop.
So instead of this:
for (int y = 0; y < 100; y++)
{
for (int i = 100 - 1; i > y; --i)
{
You might want to initalise them dynamically based the on the length of the array:
for (int y = 0; y < numbers.Length; y++)
{
for (int i = numbers.Length - 1; i > y; --i)
{
I am trying to learn two dimensional array and I wrote some basic code, but I am getting this exception. Could you tell me what am I doing wrong?
static void Main(string[] args)
{
Random rnd = new Random();
int[,] array = new int[2, 2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; i++)
{
array[i, j] = rnd.Next(0, 100);
}
}
for (int i = 0; i < array.GetLength(0); i++)
{
Console.WriteLine(array[i, 0] + "---" + array[i, 1]);
}
Console.ReadLine();
}
The problem is in your inner for-loop. In the iterator section, you're incrementing the i variable, but it should be j. Try this:
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
array[i, j] = rnd.Next(0, 100);
}
}