Re: A "valid" Bubble sort algorithm?
From: Randy Crawford (joe_at_burgershack.com)
Date: 03/11/04
- Next message: Jason: "Re: wordsearch algorithm"
- Previous message: Willem: "Re: A "valid" Bubble sort algorithm?"
- In reply to: Howard Kaikow: "A "valid" Bubble sort algorithm?"
- Next in thread: Willem: "Re: A "valid" Bubble sort algorithm?"
- Reply: Willem: "Re: A "valid" Bubble sort algorithm?"
- Reply: Howard Kaikow: "Re: A "valid" Bubble sort algorithm?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 10 Mar 2004 18:09:49 -0600
Howard Kaikow wrote:
> Yes, I know that bubble sort is not worth (ab)using, but Ill ask this
> question anyway,
>
> Using Visual Basic the BubbleLong sub below is my attempt at encoding the
> bubble sort algorithm given by program 6.4 in Algorithms in C by Robert
> Sedegewick. The BubbleModLong sub below is from an unknown source and is
> much faster than the algorithm in program 6.4.
>
> However, the code in BubbleModLong looks suspect.
> Is it even a bubble sort?
> If not, anybody recognize the critter?
>
> Public Sub BubbleLong(a() As Long, L As Long, r As Long)
> ' Sedgewick 6.4
> Dim i As Long
> Dim j As Long
> Dim temp As Long
>
> For i = L To r - 1
> For j = r To i + 1 Step -1
> If a(j - 1) > a(j) Then
> temp = a(j)
> a(j) = a(j - 1)
> a(j - 1) = temp
> End If
> Next j
> Next i
> End Sub
>
> Public Sub BubbleModLong(a() As Long, L As Long, r As Long)
> ' Sedgewick 6.4, modified
> Dim i As Long
> Dim j As Long
> Dim temp As Long
>
> For i = L To r - 1
> For j = r To i + 1 Step -1
> If a(i) > a(j) Then
> temp = a(j)
> a(j) = a(i)
> a(i) = temp
> End If
> Next j
> Next i
> End Sub
-- http://www.standards.com/; See Howard Kaikow's web site. The way to tell if an algorithm is a bubblesort is: 1) does it iterate N*N times? (where N is the number of items to sort) 2) does it immediately swap values after doing a compare? If yes to both then it's a bubblesort. A second implementation of a bubblesort should be no faster than the first, unless it, 1) reduces the number of comparisons, 2) it reduces the number of swaps, or it uses the CPU's hardware data caches better. Remember, when comparing any pair of sorts, you need to run several different data sets through each implementation before you can know if it's really faster than an alternative. In this case, try sending through 1) a set of random integers, 2) a set of forward sorted integers, 3) a set of reverse sorted integers. I also recommend that you run several different sized input sets. Using small, medium, and large sets will quickly reveal whether a sort is speeding up nicely or is speeding up more slowly than another sort. Also make sure the data set is large enough to take several seconds to complete. A 15 second run time is probably the minimum. Below 5 seconds is too short -- program startup and shutdown may have too great an impact on your run time to be accurate. Finally, be sure to write a quick validation program (or function) that verifies that your data is in fact correctly sorted. There's no point in comparing a routine that's correct with one that isn't. Randy -- Randy Crawford http://www.ruf.rice.edu/~rand rand AT rice DOT edu
- Next message: Jason: "Re: wordsearch algorithm"
- Previous message: Willem: "Re: A "valid" Bubble sort algorithm?"
- In reply to: Howard Kaikow: "A "valid" Bubble sort algorithm?"
- Next in thread: Willem: "Re: A "valid" Bubble sort algorithm?"
- Reply: Willem: "Re: A "valid" Bubble sort algorithm?"
- Reply: Howard Kaikow: "Re: A "valid" Bubble sort algorithm?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|