Thread: Merge and sort array.

  1. #16
    Registered User
    Join Date
    Sep 2006
    Quote Originally Posted by c99tutorial View Post
    That is a sorting algorithm. To see how it works, write it out in terms of "pseudocode" and then try it out with pencil and paper

    for i = 0...length-1
       for j = 0...length-1-i
          if data[i] > data[j+1]:
             swap data[i] and data[j+1]
    On paper you can draw i and j as two different arrows that point to one of the array items, each one marked with a number 0, 1, 2, ...., length-1
    I wouldn't recommend this ^^^^ algorithm. That's a Substitution sort with an offset of 1, and also goes out of bounds (off the top) of the array, because of the +1.

    I'm sure you meant data[i] > data[i+1], for the comparison and the swap.

    If you aren't comparing adjacent elements, it's not a Bubble sort - and it won't b-u-b-b-l-e. (Oh! The horror! LOL ).

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Nice, France
    I am posting again the pseudocode here, in order to avoid confusion.

    Try to read again your posts after submitting them
    Code - functions and small libraries I use

    It’s 2014 and I still use printf() for debugging.

    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #18
    Registered User
    Join Date
    Nov 2012
    Oops, I mistyped it. Ok, it should match the original one submitted. With data[j] and data[j+1] being swapped. The idea I was making is that you get rid of the "C programming language stuff" from the algorithm to make it easier to see how it actually works. For example, it's easy to know what "swap x and y" means.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Runtime Hangup with Merge Sort of 2D array
    By orlileithian in forum C Programming
    Replies: 12
    Last Post: 10-28-2012, 12:25 PM
  2. Merge Sort (Array)
    By Jack Hammer in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2011, 02:37 PM
  3. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 06:48 PM
  4. Quick sort VS Merge Sort
    By sachitha in forum Tech Board
    Replies: 7
    Last Post: 09-03-2004, 11:57 PM
  5. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM