Closed Thread
Results 1 to 10 of 10

Thread: Sorting Array's

  1. #1
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Sorting Array's

    Can anyone please write an "easy" tutorial on sorting array's? I have seen some on the internet and read some books but they are too complex for me too understand , so anyone up to make a tutorial ?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Sorting Array's

    There are many algorithms to do so; the one programmers typically use is called QuickSort or QSort. The easiest, but also probably the slowest sorting algorithm, is the Bubble Sort, which I've implemented to work only on int arrays here:

    Code:
    void bubblesort(int *data,int length)
    {
        int swapcount = 1;
        
        while(swapcount > 0)
        {
            swapcount = 0;
            for(int i = 0; i < length - 1; ++i)
            {
                if(data[i] > data[i + 1])
                {
                    int temp = data[i];
                    data[i] = data[i + 1];
                    data[i + 1] = temp;
                    ++swapcount;
                }
            }
        }
    }
    If you don't have to write your own sorting algorithm, just use the C library's qsort(), declared in stdlib.h. You can use it like so:

    Code:
    int compareFunc(const void *a, const void *b)
    {
        int *pa = (int *)a, *pb = (int *)b;
        return *pa - *pb;
    }
    
    int main()
    {
        ....
        qsort(myArray,ARRAY_LENGTH,sizeof(int),compareFunc);
        ....
    }
    All you need to do is create your own comparison function that takes two const void pointers and returns an int with the following conditions:
    1) If the first argument is greater than the second argument, return a positive integer.
    2) If the first argument is equal to the second argument, return 0.
    3) If the first argument is less than the second argument, return a negative integer.

    This can be used to sort structs, classes, anything.

    Syntax:
    qsort(pointer-to-data,number-of-elements,size-of-element,comparator)

  4. #3
    lucuis is offline Newbie
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Re: Sorting Array's

    Try this: Bubble sort tutorial. Hope it is simple enough.

  5. #4
    CPD
    CPD is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    57
    Rep Power
    12

    Re: Sorting Array's

    Everyone teaches bubble sort, but I think selection sort is more intuitive. You find the smallest value and move it to the front. Since you moved the smallest value to the front, you can skip that value from that point on. If you do that as if working with the latter portion of the array until the portion dwindles to a length of 1, you end up with a sorted list:

    5, 1, 4, 2, 3

    Start looking for the smallest value at element 0.
    Found the smallest value at element 1.
    Swap the values at element 0 and 1.

    1, 5, 4, 2, 3

    Start looking for the smallest value at element 1 because you already know that the value at element 0 is the smallest in the entire group from 0 to 4.
    Found the smallest value at element 3.
    Swap the values at element 1 and 3.

    1, 2, 4, 5, 3

    Start looking for the smallest value at element 2 because you already know that the values up to element 2 are the smallest so far.
    Found the smallest value at element 4.
    Swap the values at element 2 and 4.

    1, 2, 3, 5, 4

    Start looking for the smallest value at element 3 because you already know that the values up to element 3 are the smallest so far.
    Found the smallest value at element 4.
    Swap the values at element 3 and 4.

    1, 2, 3, 4, 5

    Stop at element 4 because there's only one value left, so it has to be the largest in the array.

    Here's some code to play with:
    Code:
    #include <stdio.h>
    
    void printa(int a[], size_t size)
    {
      size_t i;
    
      for (i = 0; i < size; i++)
        printf("%-3d", a[i]);
      puts("");
    }
    
    void selection_sort(int a[], size_t size)
    {
      size_t i;
    
      printa(a, size);
      puts("");
    
      /* From the current unsorted half */
      for (i = 0; i < size - 1; i++) {
        size_t min = i;
        size_t j;
    
        printf("Start looking for the smallest value at element %d\n", i);
    
        /* Find the smallest value in the unsorted half */
        for (j = i + 1; j < size; j++)
          if (a[j] < a[min])
            min = j;
    
        printf("Found the smallest value at element %d\n", min);
    
        /* Swap the value with the first unsorted value */
        if (min != i) {
          int temp = a[min];
          a[min] = a[i];
          a[i] = temp;
    
          printf("Swap the values at element %d and %d\n", i, min);
        }
        else
          printf("Don't need to swap because %d is the smallest\n", i);
    
        puts("");
        printa(a, size);
        puts("");
      }
    
      printf("Stop at element %d because there's only one value left, "
        "so it has to be the largest in the array\n", i);
    }
    
    int main()
    {
      int a[] = {0, 1, 9, 2, 8, 3, 7, 4, 6, 5};
    
      selection_sort(a, 10);
    
      return 0;
    }

  6. #5
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Sorting Array's

    I can see why you'd say the selection sort is more intuitive, and it is - the problem is moving the item to the front. Swapping makes the sort unstable, and shifting is time-consuming.

  7. #6
    CPD
    CPD is offline Learning Programmer
    Join Date
    Nov 2008
    Posts
    57
    Rep Power
    12

    Re: Sorting Array's

    Swapping makes the sort unstable, and shifting is time-consuming.
    That's an interesting statement seeing as you suggested qsort earlier. qsort is pretty much always implemented as quicksort or introsort, and both of those are naturally unstable as well. I wouldn't recommend making selection sort stable anyway because it removes one of the best selling points: super efficient data movement. Selection sorting blows a lot of algorithms away when the values are large and copying is expensive.

    Finally, bringing stability into the picture is both unnecessary and counterproductive, since adding another concept just makes things more complicated for ahmed, and he specifically asked for simple. Stability is something that can wait until later.

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Sorting Array's

    True, true. Quicksort can be stable, but like your selection sort, is not stable in efficient implementations. You win.

    Ahmed, just ignore what we just said until you really get this.

  9. #8
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Re: Sorting Array's

    Intresting discussion , though where ever I see mostly in books there is bubble sort but y?and I also wanted to discuss that why do we still have to learn C++ ? I really dont find this language intresting but I gota study it cause of my uni I find C# more fun and easier

  10. #9
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Sorting Array's

    C++ is used heavily throughout industry. If you look at the list of applications that are created with C++, you will find large segments of the video game industry and MicroSoft near the top of the list. MicroSoft doesn't use C# very much internally, but they want you to use it.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  11. #10
    yuyujoke is offline Newbie
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Re: Sorting Array's

    maybe this can help u

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting issue
    By Wintermute in forum C and C++
    Replies: 5
    Last Post: 04-18-2011, 07:49 AM
  2. Sorting
    By unstoppable in forum General Programming
    Replies: 3
    Last Post: 03-15-2011, 08:38 AM
  3. Sorting efficiency
    By chili5 in forum Programming Theory
    Replies: 9
    Last Post: 07-19-2010, 02:14 PM
  4. PHP and Sorting
    By chili5 in forum PHP Tutorials
    Replies: 6
    Last Post: 08-28-2009, 06:57 PM
  5. Sorting In PHP vs. DB
    By BASHERS33 in forum PHP Development
    Replies: 4
    Last Post: 07-31-2009, 09:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts