Bubble Sorting Code

Hey Guys......Go to My Blog To Get Bubble Sorting Code in c++...The Code is Very Simple And i Wrote it Just For understanding How Bubble Sorting Works ...For Any Assistance related To any code In C++,You Can Ask Me On My Blog :)
http://codesinvisualc.blogspot.com/
closed account (1vRz3TCk)
I read up to void main(), I should have stopped at #include<iostream.h> .
o_0
Last edited on
what?.....dude..thats for beginners.......who just stepped into programming.......thats not for you,you so called smart programmer.......
closed account (1vRz3TCk)
So you want to teach bad practice from the start?
What is wrong with including a standard library?
"So called"?

I don't know much about CodeMonkey's programming skill, but I can't say anyone called him a smart programmer in this thread. There's no need to get defensive.

The problem with your code is that teaches some really bad habits. <iostream.h> has been deprecated (use <iostream> instead, note that there is no .h), and void main() was never standards-compliant (and g++, one of the world's most used compilers will not compile it, demanding that the type be changed to int). In the example above the bubble sort, you're using C strings when C++ strings (std::string from <string>) are a much safer alternative. Now, if this were C, then the use of char[]s as strings would be perfectly fine. However, C++ calls for a different set of ideals and to that end offers a different set of facilities.

Sorry, but your code could be much better. :/

-Albatross
Last edited on
closed account (1vRz3TCk)
There is nothing wrong with including a standard library, iostream.h is non-standard it should be #include<iostream> .
I did notice that the .h was there, but I thought this was compiler-related usage.
guys you are right ......i should use iostream instead of adding up .h..the purpose of the code was just to give an overview of algo how bubble sorting works..thats why i used integer array for simplicity.once u know how to handle with them,then the next step is to learn about strings or char arrays using string library.....once you get an idea how to make algo n pseudo-code,then standards are not a big deal for some to get...:)
Actually, learning proper coding standards should be the priority. Any algorithm (including pseudocode) can be found on Wikipedia or after a quick google search. Proper coding is something you have to learn at the start, as unlearning bad habits is very, very difficult. Now if you'll excuse me, I'll be off declaring some global variables in a header file.
sure
Hi @Rida

You should really have a 'while' loop on the outside of your inner 'for' loop which just tests if anything changed (i.e. if the array is now sorted or not).
If I fed a 4000-element array into your algorithm that was already sorted, do you know how many times your 'if' statement would have to execute before it was able to confirm that?

Hint: 15,992,000 ;)

Regards, keineahnung

*Edit*
Otherwise your code looks fine. Keep at it :)
Last edited on
1
2
3
  for(int j=0;j<len;j++)
  {
   if(array[j]>array[j+1] && (j+1)<len)// out of bounds 


Funny thing. "quicksort" has a worst case O(n^2), that happends for a sorted array.
ne555 wrote:
Funny thing. "quicksort" has a worst case O(n^2), that happends for a sorted array.

I'm think that's only if you pick the first (or last) element as the pivot.
Topic archived. No new replies allowed.