Thanks

Thanks

- Home
- »
- C# Language
- »
- Bubble Sort c#

Bubble Sort c#

Posted on: 30 Aug 2011

Posted on: 30 Aug 2011

Hello guys...I need help wth a c# program(Console plz) where im asked to show bubble sort in these conditions: the array is of 5000 int elements,reverse sorted,already sorted and randomly sorted. The output shud show:number of comparisons and how many swaps were performed. I need the same for insertion and selection sort but I just need help wth one of them il figure out the rest.

Thanks

Thanks

Thanks

AuthorReply

accepted

Re: Bubble Sort c#

Posted on: 30 Aug 2011**Accepted Answer**

Posted on: 30 Aug 2011

This code should do it:

using System;

class Bubble

{

static Random rand = new Random();

static void Main()

{

// create an array of 5000 random integers between 0 and 4999 inclusive

int[] a = new int[5000];

for(int i = 0; i < 5000; i++) a[i] = rand.Next(5000);

int numComps;

int numSwaps;

Console.Clear();

Console.WriteLine("Results for randomly sorted array\n");

BubbleSort(a, out numComps, out numSwaps);

PrintResults(numComps, numSwaps);

Console.WriteLine("Results for already sorted array\n");

BubbleSort(a, out numComps, out numSwaps);

PrintResults(numComps, numSwaps);

Array.Reverse(a);

Console.WriteLine("Results for reverse sorted array\n");

BubbleSort(a, out numComps, out numSwaps);

PrintResults(numComps, numSwaps);

Console.ReadKey();

}

static void BubbleSort(int[] a, out int numComps, out int numSwaps)

{

numComps = 0;

numSwaps = 0;

int n = a.Length;

do

{

int newN = 0;

for(int i = 1; i < n; i++)

{

numComps++;

if (a[i-1] > a[i])

{

numSwaps++;

int temp = a[i-1];

a[i-1] = a[i];

a[i] = temp;

newN = i;

}

}

n = newN;

}

while(n > 0);

}

static void PrintResults(int numComps, int numSwaps)

{

Console.WriteLine(" Number of comparisons = {0}", numComps);

Console.WriteLine(" Number of swaps = {0}", numSwaps);

Console.WriteLine();

}

}

Re: Bubble Sort c#

Posted on: 30 Aug 2011

Posted on: 30 Aug 2011

I think Vulpes did your homework for you but does it really help you learn? Are you going to be able to figure out other problems yourself in the future?

I know this is a class assignment because a bubble sort is so inefficient that its use is impractical for real-world problems.

I know this is a class assignment because a bubble sort is so inefficient that its use is impractical for real-world problems.

Thinking is a feeling; pleasant for some and unpleasant for others.

Re: Bubble Sort c#

Posted on: 01 Sep 2011

Posted on: 01 Sep 2011

Thanks Vulpes ur solutions really help coz they show a different approach to how I would do things and how my lecturer does them as well and I like that they are easy to understand.

Thanks

Re: Bubble Sort c#

Posted on: 01 Sep 2011

Posted on: 01 Sep 2011

Thanks for that Sam I completely get u but u dont have to worry because this isnt really homework. We have been doing these sorts in pseudocode and I knw how they work. The lecturer just wanted us to compare them for our own knowledge we wont hand it in. So he said we can do it in c# code so that we see how they work and so on(as it says in the question) but I totally understand where u come from and I appreciate it but really there is no harm done

Thanks

Re: Bubble Sort c#

Posted on: 01 Sep 2011

Posted on: 01 Sep 2011

A bubble sort is easy. It can be done using very few resources in the sense of very limited memory. Back in the days that memory was immensely more limited, programmers would use the trick of swapping data by doing three exclusive-ors without using a temporary storage space.

The challenge is to do a binary sort. That is a little more complicated and more efficient. Something even more challenging is to do binary sort and update the array with new entries when they are encountered. The one I wrote like that was written in about 1974 using COBOL. I re-wrote a program to do a dynamically-built array and binary sort as I described and improved the efficiency of the program significantly. I appreciate however that modern languages such as C# and databases have very efficient sorting functions.

So in other words, read a file and do a binary search and when the item is not found, add it to the array in the proper sort order. Most binary search algorithms do not include the dynamic update such as taht.

For studying sorting Sorting and Searching Algorithms is a very interesting site. On that page, click in the "InsertSort" box then the "ShellSort" box then the "QuickSort" box. You will see each of the three algorithms in action. I don't know if an InsertSort is the same as a bubble sort but it looks like it is.

For doing a look-up of data that needs to also be dynamically updated, a b-tree would be useful.

The challenge is to do a binary sort. That is a little more complicated and more efficient. Something even more challenging is to do binary sort and update the array with new entries when they are encountered. The one I wrote like that was written in about 1974 using COBOL. I re-wrote a program to do a dynamically-built array and binary sort as I described and improved the efficiency of the program significantly. I appreciate however that modern languages such as C# and databases have very efficient sorting functions.

So in other words, read a file and do a binary search and when the item is not found, add it to the array in the proper sort order. Most binary search algorithms do not include the dynamic update such as taht.

For studying sorting Sorting and Searching Algorithms is a very interesting site. On that page, click in the "InsertSort" box then the "ShellSort" box then the "QuickSort" box. You will see each of the three algorithms in action. I don't know if an InsertSort is the same as a bubble sort but it looks like it is.

For doing a look-up of data that needs to also be dynamically updated, a b-tree would be useful.

Thinking is a feeling; pleasant for some and unpleasant for others.

Re: Bubble Sort c#

Posted on: 02 Sep 2011

Posted on: 02 Sep 2011

thanks for that Sam i'll check it out

Thanks

Custom Software Development

MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.