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.










share|improve this question


















  • 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 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















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.










share|improve this question


















  • 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 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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














  • 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 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








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












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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 at 8:26

























        answered Nov 19 at 18:58









        Alex Leo

        25210




        25210






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Ottavio Pratesi

            Tricia Helfer

            15 giugno