Thread: shell sort example in "Advanced C"

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    2

    shell sort example in "Advanced C"

    Hi, I'm starting to get into C and am reading "Advanced C" by Herbert Schildt. He uses this example of the shell sort (K&R style?):

    Code:
    void shell(item, count)
    char *item;
    int count;
    {
    register int i, j, k, s, w; char x, a[5]; a[0]=9; a[1]=5; a[2]=3; a[3]=2; a[4]=1; for(w=0; w<5; w++) {
    k = a[w]; s = -k; for(i=k; i<count; ++i) {
    x = item[i]; j = i-k; if(s==0) { s = -k;
    s++; item[s] = x;
    } while(x<item[j] && j>=0 && j <=count) {
    item[j+k] = item[j]; j = j-k;
    } item[j+k] = x;
    }
    }
    }
    so my question is what is 's' for? why would it ever be 0? thanks

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_O

    Wow.

    You are in desperate need of a different book.

    Soma

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    To further explain Soma's comment a bit, the author Schildt sucks so badly, they made up a word for it: bullschildt.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    There are better sort methods like quick sort or merge sort, but here is an example of a shell sort. This example is either from an old book or from some web article:

    Code:
    void shell_sort(int *a, int n)
    {
    int h, i, j, k;     /* h is "gap" size */
    
        for (h = n; h /= 2;){
            for (i = h; i < n; i++){
                k = a[i];
                for (j = i; j >= h && k < a[j - h]; j -= h){
                    a[j] = a[j - h];
                }
                a[j] = k;
            }
        }
    }
    
    int main(int argc, char *argv[])
    {
    int a[] = {41, 53, 26, 0, 67, 98, 39, 84, 72, 15};
    int n = sizeof(a) / sizeof(a[0]);
    
        shell_sort(a, n);
    
        return 0;
    }
    Wiki article about shell sort, including variations on the gap size (which is what "h" is used for in the above example):

    http://en.wikipedia.org/wiki/Shell_sort
    Last edited by rcgldr; 06-14-2013 at 08:11 PM.

  5. #5
    Registered User
    Join Date
    Jun 2013
    Posts
    2
    I'd heard of schildts reputation but I suppose I was just curious of his thought process. guess it's undecipherable here. Thanks again

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's so badly written that it's literally painful to read.

    To answer your original question: s doesn't seem to be doing anything and I can't imagine how it would ever become 0.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by oogabooga
    It's so badly written that it's literally painful to read.
    I had the impression that they are usually stylistically well written and hence a pleasure to read... if you enjoy fiction.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I had the impression that they are usually stylistically well written and hence a pleasure to read... if you enjoy fiction.
    ^_^v

    Delicious!

    Soma

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    When I read this question, I expected more Bourne-syntax and stuff lol.

    I've actually never once heard of shell sorting before this.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by MutantJohn View Post
    When I read this question, I expected more Bourne-syntax and stuff lol.

    I've actually never once heard of shell sorting before this.
    When T-Rex ruled the food chain, Shell sort was the top dog in-place comparison sorter. This was before Quicksort became well known, and some quirks in picking the pivot were worked out.

    Shell sort uses Insertion sort, but the changing gap helps it to move many values a larger distance, with far fewer comparisons. As cache sizes have increased, Shell sort performance has improved. Until you get past 50,000 numbers, it's hard to beat Shell sort.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by laserlight View Post
    I had the impression that they are usually stylistically well written and hence a pleasure to read... if you enjoy fiction.
    I tend to be a bit more generous to Schildt than others. I would compare his writing style to that of a fourth-grade science book - oversimplified and imprecise, but with the intent to deliver some key ideas without being bogged down in details. In that kind of niche, his work has a use for someone still learning the very basics. Once you've learned the fundamentals, you will have little use for his work. It's about as useful as a reference guide for a serious programmer as One Fish, Two Fish is for an ichthyologist.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You don't have to dumb down science for 4th graders, you just have to be Bill Nye.

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by Cat View Post
    I tend to be a bit more generous to Schildt than others. I would compare his writing style to that of a fourth-grade science book - oversimplified and imprecise, but with the intent to deliver some key ideas without being bogged down in details. In that kind of niche, his work has a use for someone still learning the very basics. Once you've learned the fundamentals, you will have little use for his work. It's about as useful as a reference guide for a serious programmer as One Fish, Two Fish is for an ichthyologist.
    As the author of a programming book myself, I've got quite a bit of sympathy for Schildt.

    Schildt writes a beginner's guide to C, and he's quite readable. That's why the books are so popular. A lot of the time, you actually make a point harder to understand by insisting on complete and total accuracy. Some of the mistakes are just mistakes, but even here, arguably they make the book better. "The textbook is always right" is a great enemy of education.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    A lot of the time, you actually make a point harder to understand by insisting on complete and total accuracy.
    O_O

    THIS IS PROGRAMMING!

    The compiler and environment will insist on complete and total accuracy.

    Some of the mistakes are just mistakes, but even here, arguably they make the book better.
    No.

    Mistakes are only arguably forgivable, but mistakes absolutely never make a programming book better.

    That's why good writers and publishers produce freely available errata long after the book is no longer sold.

    [Edit]
    Oh, and before anyone reaches for "What about examples that are supposed to have syntax errors or similar?", imagine how frustrating it would be to have a "Correct this example code." style exercises which actually have no errors yet have corrections listed in supplemental materials. No, that does not help; it spreads a disease of incompetence among the new individuals which is precisely what good instruction texts alleviates.
    [/Edit]

    Soma

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Programming books don't have to include every possible detail in their discussions, but the examples must compile, run, and produce accurate results.

    Mistakes in a programming example, do NOT make a programming book, better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why some linux shell doesn't support "Delete" Key?
    By meili100 in forum Tech Board
    Replies: 18
    Last Post: 09-30-2008, 07:09 AM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM
  4. "Sam's teach yourself advanced C in 21 days"
    By Nutshell in forum C Programming
    Replies: 12
    Last Post: 10-07-2002, 09:43 PM
  5. command line "shell"
    By mart_man00 in forum C Programming
    Replies: 3
    Last Post: 09-29-2002, 01:29 AM