Click here to Skip to main content
15,885,782 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Incomprehensible C++ error messages Pin
Stefan_Lang8-Mar-20 23:59
Stefan_Lang8-Mar-20 23:59 
GeneralRe: Incomprehensible C++ error messages Pin
John R. Shaw26-Mar-20 20:28
John R. Shaw26-Mar-20 20:28 
Generalwhere can i post my course url here without being banned? Pin
FatalError0x4c28-Jan-20 21:59
FatalError0x4c28-Jan-20 21:59 
GeneralRe: where can i post my course url here without being banned? Pin
GKP199228-Jan-20 22:05
professionalGKP199228-Jan-20 22:05 
GeneralRe: where can i post my course url here without being banned? Pin
Richard MacCutchan28-Jan-20 22:13
mveRichard MacCutchan28-Jan-20 22:13 
GeneralRe: where can i post my course url here without being banned? Pin
GKP199228-Jan-20 22:38
professionalGKP199228-Jan-20 22:38 
GeneralRe: where can i post my course url here without being banned? Pin
Richard MacCutchan28-Jan-20 22:14
mveRichard MacCutchan28-Jan-20 22:14 
GeneralBubble Sort, O(N^2) aka Quadratic time Pin
raddevus27-Jan-20 5:38
mvaraddevus27-Jan-20 5:38 
I'm reading the book, A Common-Sense Guide to Data Structures and Algorithms: Level Up Your Core Programming Skills[^]
The book has some code samples in Ruby!! Dead | X| Argh!
I converted the first one to C# for you. Although I'm sure most of you have written a bubble sort before and could do it blindfolded and typing only with your mouse.

Of course, Bubble sort is slow and not a great algorithm. But now I understand why far better because of the book's explanation that it's O(N^2) -- runs in quadratic time. Large sets sorted this way will become extremely slow.
And this nice graph from the book shows that too : https://i.stack.imgur.com/LPdjF.png[^]
I'm quite excited to pull this stuff together into some understanding after many years of not quite being able to explain it. I know that is lame. Blush | :O

FYI if you get the Free LINQPad (LINQPad - The .NET Programmer's Playground[^]) you can copy the code below and run it easily.
C#
void bubble_sort(int [] allValues, bool showIntermediateValues = false){
	bool sorted = false;
	int stepCounter = 0;
	while (!sorted){
		sorted = true;
		for (int i = 0; i < allValues.Length-1;i++){
			if (allValues[i] > allValues[i+1]){
				sorted = false;
				int currentVal = allValues[i];
				allValues[i] = allValues[i+1];
				allValues[i+1] = currentVal;
			}
			if (showIntermediateValues){
				Console.Write($"{allValues[i]} ");
			}
		}
		if (showIntermediateValues){
			Console.WriteLine($" ## Step {++stepCounter} ##");
		}
	}
}

void Main()
{
        // ##### Driving (main) program so you can see the algorithm in action #####
	int [] allValues = {33,12,10,4,16,44};
	bubble_sort(allValues,true);
	// print the array to insure it is sorted
	TestArray(allValues);
	int [] moreValues = {100,88,38,53,14,55,40,7,2};
	bubble_sort(moreValues, true);
	TestArray(moreValues);
	
	int [] presortedValues = {5,17,33,59,72,73,74,75,76,88,99,100};
	bubble_sort(presortedValues, true);
	TestArray(presortedValues);

}

void TestArray(int [] allValues){
	Console.WriteLine("############## OUTPUT #################");
	Console.Write("SORTED ===> ");
	foreach (int i in allValues){
		Console.Write($"{i} ");
	}
	Console.WriteLine();
	Console.WriteLine("#######################################");
	Console.WriteLine();
}
Output Shown With Steps
// Each Test array is shown with its steps so you can see how inefficient the bubble sort algo is.
12 10 4 16 33  ## Step 1 ##
10 4 12 16 33  ## Step 2 ##
4 10 12 16 33  ## Step 3 ##
4 10 12 16 33  ## Step 4 ##
############## OUTPUT #################
SORTED ===> 4 10 12 16 33 44 
#######################################

88 38 53 14 55 40 7 2  ## Step 1 ##
38 53 14 55 40 7 2 88  ## Step 2 ##
38 14 53 40 7 2 55 88  ## Step 3 ##
14 38 40 7 2 53 55 88  ## Step 4 ##
14 38 7 2 40 53 55 88  ## Step 5 ##
14 7 2 38 40 53 55 88  ## Step 6 ##
7 2 14 38 40 53 55 88  ## Step 7 ##
2 7 14 38 40 53 55 88  ## Step 8 ##
2 7 14 38 40 53 55 88  ## Step 9 ##
############## OUTPUT #################
SORTED ===> 2 7 14 38 40 53 55 88 100 
#######################################

5 17 33 59 72 73 74 75 76 88 99  ## Step 1 ##
############## OUTPUT #################
SORTED ===> 5 17 33 59 72 73 74 75 76 88 99 100 
#######################################

GeneralRe: Bubble Sort, O(N^2) aka Quadratic time PinPopular
Nelek27-Jan-20 8:01
protectorNelek27-Jan-20 8:01 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus27-Jan-20 8:10
mvaraddevus27-Jan-20 8:10 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
Nelek27-Jan-20 8:15
protectorNelek27-Jan-20 8:15 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus27-Jan-20 8:38
mvaraddevus27-Jan-20 8:38 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
agolddog29-Jan-20 3:26
agolddog29-Jan-20 3:26 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus29-Jan-20 4:23
mvaraddevus29-Jan-20 4:23 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus27-Jan-20 10:27
mvaraddevus27-Jan-20 10:27 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
Nelek27-Jan-20 12:53
protectorNelek27-Jan-20 12:53 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
Sander Rossel9-Mar-20 0:15
professionalSander Rossel9-Mar-20 0:15 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
Nelek9-Mar-20 1:42
protectorNelek9-Mar-20 1:42 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
Jon McKee27-Jan-20 13:35
professionalJon McKee27-Jan-20 13:35 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus28-Jan-20 1:39
mvaraddevus28-Jan-20 1:39 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
Amarnath S27-Jan-20 13:57
professionalAmarnath S27-Jan-20 13:57 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus28-Jan-20 1:41
mvaraddevus28-Jan-20 1:41 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
maze328-Jan-20 22:34
professionalmaze328-Jan-20 22:34 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
raddevus29-Jan-20 2:14
mvaraddevus29-Jan-20 2:14 
GeneralRe: Bubble Sort, O(N^2) aka Quadratic time Pin
RugbyLeague29-Jan-20 21:17
RugbyLeague29-Jan-20 21:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.