Bubble Sort Program
up vote
1
down vote
favorite
I am creating a bubble sort program which sorts random integers in an array. The array is supposed to be able to hold up to a million sorted integers. When I get to a high number (for example, 250,000) the program will sit there and never output anything. Code is below:
using System;
namespace SortingProject
{
class MainClass
{
public static void Main(string args)
{
//create an array to hold integers
int list = new int[50000];
//call random function to populate integer values
Random rand = new Random();
//add integers to array
for (int i = 0; i < list.Length; i++) {
list[i] = rand.Next(1,50000);
}
//call bubble sort method and input the array
BubbleSorting(list);
}
//bubble sort method and logic
public static void BubbleSorting(int array)
{
//initialize time start
DateTime start = DateTime.Now;
DateTime end;
end = DateTime.Now;
//initialize element integer
int element = 0;
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
if (array[sort] > array[sort + 1])
{
element = array[sort + 1];
array[sort + 1] = array[sort];
array[sort] = element;
}
}
}
//loop and print array contents
for (int i = 0; i < array.Length; i++)
Console.Write(array[i] + " ");
//calculate time it takes to sort array
end = DateTime.Now;
TimeSpan ts = end.Subtract(start);
Console.WriteLine(" ");
Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
}
}
}
I usually wait for awhile while the program is running but it seems to be freezing with larger arrays. Any help on why would be greatly appreciated.
c# arrays sorting bubble-sort
|
show 3 more comments
up vote
1
down vote
favorite
I am creating a bubble sort program which sorts random integers in an array. The array is supposed to be able to hold up to a million sorted integers. When I get to a high number (for example, 250,000) the program will sit there and never output anything. Code is below:
using System;
namespace SortingProject
{
class MainClass
{
public static void Main(string args)
{
//create an array to hold integers
int list = new int[50000];
//call random function to populate integer values
Random rand = new Random();
//add integers to array
for (int i = 0; i < list.Length; i++) {
list[i] = rand.Next(1,50000);
}
//call bubble sort method and input the array
BubbleSorting(list);
}
//bubble sort method and logic
public static void BubbleSorting(int array)
{
//initialize time start
DateTime start = DateTime.Now;
DateTime end;
end = DateTime.Now;
//initialize element integer
int element = 0;
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
if (array[sort] > array[sort + 1])
{
element = array[sort + 1];
array[sort + 1] = array[sort];
array[sort] = element;
}
}
}
//loop and print array contents
for (int i = 0; i < array.Length; i++)
Console.Write(array[i] + " ");
//calculate time it takes to sort array
end = DateTime.Now;
TimeSpan ts = end.Subtract(start);
Console.WriteLine(" ");
Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
}
}
}
I usually wait for awhile while the program is running but it seems to be freezing with larger arrays. Any help on why would be greatly appreciated.
c# arrays sorting bubble-sort
3
Could it be that you simply understimate that the time to complete scales literally exponentially O(n^2)?
– Christopher
Nov 19 at 0:20
Note that bubble sort is O(n^2) in running time complexity. Meaning, as you increase the input size (list length) the computation takes more and more time to compute the answer. You should see a super-linear increase in running time with larger inputs. Have you tried some other inputs to see if you can find a length where it stops working?
– Jochem Kuijpers
Nov 19 at 0:20
for every element in the array, do something with every element in the array, think about how that scales. type in to your calculator, number of elements then hit the x2 button, see what it does
– TheGeneral
Nov 19 at 0:21
2
You could place break points to understand where your code is spending all it's time OR since this is a console app, you could add a line like if (bubble % 1000 == 0) Console.WriteLine($"Still running: {bubble}"); to your bubble for loop. It's not dead, it just needs more time to process.
– David Yates
Nov 19 at 0:32
1
For your future reference, I suggest you use theStopwatchclass rather thanDateTime.Nowfor timing code.DateTime.Nowis only precise to a few milliseconds, which is fine for your situation where you are running programs that take seconds, minutes or hours. But get in the habit of usingStopwatchfor your timing needs, as it is far more precise.
– Eric Lippert
Nov 19 at 19:04
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am creating a bubble sort program which sorts random integers in an array. The array is supposed to be able to hold up to a million sorted integers. When I get to a high number (for example, 250,000) the program will sit there and never output anything. Code is below:
using System;
namespace SortingProject
{
class MainClass
{
public static void Main(string args)
{
//create an array to hold integers
int list = new int[50000];
//call random function to populate integer values
Random rand = new Random();
//add integers to array
for (int i = 0; i < list.Length; i++) {
list[i] = rand.Next(1,50000);
}
//call bubble sort method and input the array
BubbleSorting(list);
}
//bubble sort method and logic
public static void BubbleSorting(int array)
{
//initialize time start
DateTime start = DateTime.Now;
DateTime end;
end = DateTime.Now;
//initialize element integer
int element = 0;
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
if (array[sort] > array[sort + 1])
{
element = array[sort + 1];
array[sort + 1] = array[sort];
array[sort] = element;
}
}
}
//loop and print array contents
for (int i = 0; i < array.Length; i++)
Console.Write(array[i] + " ");
//calculate time it takes to sort array
end = DateTime.Now;
TimeSpan ts = end.Subtract(start);
Console.WriteLine(" ");
Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
}
}
}
I usually wait for awhile while the program is running but it seems to be freezing with larger arrays. Any help on why would be greatly appreciated.
c# arrays sorting bubble-sort
I am creating a bubble sort program which sorts random integers in an array. The array is supposed to be able to hold up to a million sorted integers. When I get to a high number (for example, 250,000) the program will sit there and never output anything. Code is below:
using System;
namespace SortingProject
{
class MainClass
{
public static void Main(string args)
{
//create an array to hold integers
int list = new int[50000];
//call random function to populate integer values
Random rand = new Random();
//add integers to array
for (int i = 0; i < list.Length; i++) {
list[i] = rand.Next(1,50000);
}
//call bubble sort method and input the array
BubbleSorting(list);
}
//bubble sort method and logic
public static void BubbleSorting(int array)
{
//initialize time start
DateTime start = DateTime.Now;
DateTime end;
end = DateTime.Now;
//initialize element integer
int element = 0;
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
if (array[sort] > array[sort + 1])
{
element = array[sort + 1];
array[sort + 1] = array[sort];
array[sort] = element;
}
}
}
//loop and print array contents
for (int i = 0; i < array.Length; i++)
Console.Write(array[i] + " ");
//calculate time it takes to sort array
end = DateTime.Now;
TimeSpan ts = end.Subtract(start);
Console.WriteLine(" ");
Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
}
}
}
I usually wait for awhile while the program is running but it seems to be freezing with larger arrays. Any help on why would be greatly appreciated.
c# arrays sorting bubble-sort
c# arrays sorting bubble-sort
asked Nov 19 at 0:12
Zeze
81
81
3
Could it be that you simply understimate that the time to complete scales literally exponentially O(n^2)?
– Christopher
Nov 19 at 0:20
Note that bubble sort is O(n^2) in running time complexity. Meaning, as you increase the input size (list length) the computation takes more and more time to compute the answer. You should see a super-linear increase in running time with larger inputs. Have you tried some other inputs to see if you can find a length where it stops working?
– Jochem Kuijpers
Nov 19 at 0:20
for every element in the array, do something with every element in the array, think about how that scales. type in to your calculator, number of elements then hit the x2 button, see what it does
– TheGeneral
Nov 19 at 0:21
2
You could place break points to understand where your code is spending all it's time OR since this is a console app, you could add a line like if (bubble % 1000 == 0) Console.WriteLine($"Still running: {bubble}"); to your bubble for loop. It's not dead, it just needs more time to process.
– David Yates
Nov 19 at 0:32
1
For your future reference, I suggest you use theStopwatchclass rather thanDateTime.Nowfor timing code.DateTime.Nowis only precise to a few milliseconds, which is fine for your situation where you are running programs that take seconds, minutes or hours. But get in the habit of usingStopwatchfor your timing needs, as it is far more precise.
– Eric Lippert
Nov 19 at 19:04
|
show 3 more comments
3
Could it be that you simply understimate that the time to complete scales literally exponentially O(n^2)?
– Christopher
Nov 19 at 0:20
Note that bubble sort is O(n^2) in running time complexity. Meaning, as you increase the input size (list length) the computation takes more and more time to compute the answer. You should see a super-linear increase in running time with larger inputs. Have you tried some other inputs to see if you can find a length where it stops working?
– Jochem Kuijpers
Nov 19 at 0:20
for every element in the array, do something with every element in the array, think about how that scales. type in to your calculator, number of elements then hit the x2 button, see what it does
– TheGeneral
Nov 19 at 0:21
2
You could place break points to understand where your code is spending all it's time OR since this is a console app, you could add a line like if (bubble % 1000 == 0) Console.WriteLine($"Still running: {bubble}"); to your bubble for loop. It's not dead, it just needs more time to process.
– David Yates
Nov 19 at 0:32
1
For your future reference, I suggest you use theStopwatchclass rather thanDateTime.Nowfor timing code.DateTime.Nowis only precise to a few milliseconds, which is fine for your situation where you are running programs that take seconds, minutes or hours. But get in the habit of usingStopwatchfor your timing needs, as it is far more precise.
– Eric Lippert
Nov 19 at 19:04
3
3
Could it be that you simply understimate that the time to complete scales literally exponentially O(n^2)?
– Christopher
Nov 19 at 0:20
Could it be that you simply understimate that the time to complete scales literally exponentially O(n^2)?
– Christopher
Nov 19 at 0:20
Note that bubble sort is O(n^2) in running time complexity. Meaning, as you increase the input size (list length) the computation takes more and more time to compute the answer. You should see a super-linear increase in running time with larger inputs. Have you tried some other inputs to see if you can find a length where it stops working?
– Jochem Kuijpers
Nov 19 at 0:20
Note that bubble sort is O(n^2) in running time complexity. Meaning, as you increase the input size (list length) the computation takes more and more time to compute the answer. You should see a super-linear increase in running time with larger inputs. Have you tried some other inputs to see if you can find a length where it stops working?
– Jochem Kuijpers
Nov 19 at 0:20
for every element in the array, do something with every element in the array, think about how that scales. type in to your calculator, number of elements then hit the x2 button, see what it does
– TheGeneral
Nov 19 at 0:21
for every element in the array, do something with every element in the array, think about how that scales. type in to your calculator, number of elements then hit the x2 button, see what it does
– TheGeneral
Nov 19 at 0:21
2
2
You could place break points to understand where your code is spending all it's time OR since this is a console app, you could add a line like if (bubble % 1000 == 0) Console.WriteLine($"Still running: {bubble}"); to your bubble for loop. It's not dead, it just needs more time to process.
– David Yates
Nov 19 at 0:32
You could place break points to understand where your code is spending all it's time OR since this is a console app, you could add a line like if (bubble % 1000 == 0) Console.WriteLine($"Still running: {bubble}"); to your bubble for loop. It's not dead, it just needs more time to process.
– David Yates
Nov 19 at 0:32
1
1
For your future reference, I suggest you use the
Stopwatch class rather than DateTime.Now for timing code. DateTime.Now is only precise to a few milliseconds, which is fine for your situation where you are running programs that take seconds, minutes or hours. But get in the habit of using Stopwatch for your timing needs, as it is far more precise.– Eric Lippert
Nov 19 at 19:04
For your future reference, I suggest you use the
Stopwatch class rather than DateTime.Now for timing code. DateTime.Now is only precise to a few milliseconds, which is fine for your situation where you are running programs that take seconds, minutes or hours. But get in the habit of using Stopwatch for your timing needs, as it is far more precise.– Eric Lippert
Nov 19 at 19:04
|
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
Tested the code and it runs fine (tested 250000 values). As pointed in the comments The Bubble Sort algorithm is not the most optimized one. Its complexity is given by:
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
\do logic
}
}
The outer for loop will do N loops. The inner for loop will do N loops.
The big O notation will have a complexity of N*N , therefore we have O(N^2).
With 250000 values there will be 62,500,000,000 iterations.
Keeping that in mind the complexity (time taken if you will) is directly proportional to the number of values N, the more values you need to sort the longer the Bubble sort will take to complete.
Hope it helps.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Tested the code and it runs fine (tested 250000 values). As pointed in the comments The Bubble Sort algorithm is not the most optimized one. Its complexity is given by:
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
\do logic
}
}
The outer for loop will do N loops. The inner for loop will do N loops.
The big O notation will have a complexity of N*N , therefore we have O(N^2).
With 250000 values there will be 62,500,000,000 iterations.
Keeping that in mind the complexity (time taken if you will) is directly proportional to the number of values N, the more values you need to sort the longer the Bubble sort will take to complete.
Hope it helps.
add a comment |
up vote
1
down vote
Tested the code and it runs fine (tested 250000 values). As pointed in the comments The Bubble Sort algorithm is not the most optimized one. Its complexity is given by:
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
\do logic
}
}
The outer for loop will do N loops. The inner for loop will do N loops.
The big O notation will have a complexity of N*N , therefore we have O(N^2).
With 250000 values there will be 62,500,000,000 iterations.
Keeping that in mind the complexity (time taken if you will) is directly proportional to the number of values N, the more values you need to sort the longer the Bubble sort will take to complete.
Hope it helps.
add a comment |
up vote
1
down vote
up vote
1
down vote
Tested the code and it runs fine (tested 250000 values). As pointed in the comments The Bubble Sort algorithm is not the most optimized one. Its complexity is given by:
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
\do logic
}
}
The outer for loop will do N loops. The inner for loop will do N loops.
The big O notation will have a complexity of N*N , therefore we have O(N^2).
With 250000 values there will be 62,500,000,000 iterations.
Keeping that in mind the complexity (time taken if you will) is directly proportional to the number of values N, the more values you need to sort the longer the Bubble sort will take to complete.
Hope it helps.
Tested the code and it runs fine (tested 250000 values). As pointed in the comments The Bubble Sort algorithm is not the most optimized one. Its complexity is given by:
for (int bubble = 0; bubble < array.Length; bubble++)
{
//create for loop to perform bubble sort
for (int sort = 0; sort < array.Length - 1; sort++)
{
\do logic
}
}
The outer for loop will do N loops. The inner for loop will do N loops.
The big O notation will have a complexity of N*N , therefore we have O(N^2).
With 250000 values there will be 62,500,000,000 iterations.
Keeping that in mind the complexity (time taken if you will) is directly proportional to the number of values N, the more values you need to sort the longer the Bubble sort will take to complete.
Hope it helps.
edited Nov 20 at 8:26
answered Nov 19 at 18:58
Alex Leo
25210
25210
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366771%2fbubble-sort-program%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Could it be that you simply understimate that the time to complete scales literally exponentially O(n^2)?
– Christopher
Nov 19 at 0:20
Note that bubble sort is O(n^2) in running time complexity. Meaning, as you increase the input size (list length) the computation takes more and more time to compute the answer. You should see a super-linear increase in running time with larger inputs. Have you tried some other inputs to see if you can find a length where it stops working?
– Jochem Kuijpers
Nov 19 at 0:20
for every element in the array, do something with every element in the array, think about how that scales. type in to your calculator, number of elements then hit the x2 button, see what it does
– TheGeneral
Nov 19 at 0:21
2
You could place break points to understand where your code is spending all it's time OR since this is a console app, you could add a line like if (bubble % 1000 == 0) Console.WriteLine($"Still running: {bubble}"); to your bubble for loop. It's not dead, it just needs more time to process.
– David Yates
Nov 19 at 0:32
1
For your future reference, I suggest you use the
Stopwatchclass rather thanDateTime.Nowfor timing code.DateTime.Nowis only precise to a few milliseconds, which is fine for your situation where you are running programs that take seconds, minutes or hours. But get in the habit of usingStopwatchfor your timing needs, as it is far more precise.– Eric Lippert
Nov 19 at 19:04