Like Tree1Likes

Having trouble sorting an Array that has a zero in it.

This is a discussion on Having trouble sorting an Array that has a zero in it. within the C Programming forums, part of the General Programming Boards category; Originally Posted by AndrewHunter As for the braces I didn't see the need to do the swap if none is ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    Quote Originally Posted by AndrewHunter
    As for the braces I didn't see the need to do the swap if none is required.
    Admirable, but your algorithm is wrong, or at least not selection sort. Notice that when you find an element smaller than the currently known smallest, you swap it with the current element. However, you then set smallest to the index of the currently known smallest... but wait, the currently known smallest has been swapped, so smallest is now the index of the current element, and in fact current is the index of the currently known smallest.

    If you wanted to avoid a swap if none is required, swap if smallest != current.
    Cgmgator likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Edit: Nevermind, I see. Thanks Laser!!!!

    Now I am off to brush up on some much needed reading. After all I will eventually need to tackle those "ironwood trees".
    Last edited by AndrewHunter; 06-29-2011 at 01:21 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    13
    I just wanna thank you guys, lazer, whiteflags, and Mr. Hunter. I've been working on this program pretty much all day again today. I only have a few bugs left and to clean it up. Can't thank you guys enough. UPDATE...a lot of my problems came from when I initialized last. I did it when count was still at zero, so last being count - 1, was my nightmare. I'm still have a slight problem with my flush code, but it works most of the time, so it's gotta be isolated to a few specific instances, right? I may stick up a thread to see if anybody can spot something obvious.

    Thanks guys,

    gator

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  2. Replies: 9
    Last Post: 04-07-2010, 10:03 PM
  3. Trouble sorting a list
    By pliang in forum C++ Programming
    Replies: 1
    Last Post: 07-17-2005, 06:54 AM
  4. 2D array sorting from min. to max.
    By khaled_helmy in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:17 PM
  5. Trouble with array sorting
    By dcj1978 in forum C++ Programming
    Replies: 3
    Last Post: 09-19-2003, 12:16 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21