Insertion Sort Counting Comparisons

This is a discussion on Insertion Sort Counting Comparisons within the C++ Programming forums, part of the General Programming Boards category; I'm trying to print the number of comparisons made when doing an insertion sort and the program compiles without a ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Insertion Sort Counting Comparisons

    I'm trying to print the number of comparisons made when doing an insertion sort and the program compiles without a problem but after being prompted to type in however many numbers are necessary, when I input numbers nothing happens.

    Code:
    #include<iostream>
    using namespace std;
    
    #define MAX_LIST_LEN  100
    
    int main(){
    
       int    n,                    // list length
              j,                    // loop control
              L,                   // index of maximum element in unsorted section
              a[MAX_LIST_LEN];   // array to be sorted
      // Get values for n and list.
       cout << "Enter list length (must be less than or equal to "
            << MAX_LIST_LEN << "): ";
       cin  >> n;
       cout << "Enter " << n << " numbers:" << endl;
       int count = 0;
       for (int L = 2; L<n; L++){
       while (L <= n)
         j = L;
    while (j>=2
         )a[j]<a[j-1];
            a [j] == a[j-1];
            j = j-1;
         int L = L+1;
       while (L>=1&&++count&&a[j]<a[j-1]
       )cout << count << " ";
    
       return(0);
    }
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    How many different variables called L do you have?

    How many do you need?

    Which one is in scope at any given point in the code?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    I cut the variables down to one but now the program won't compile at all:

    Code:
    #include<iostream>
    using namespace std;
    
    #define MAX_LIST_LEN  2000
    
    int main(){
    
       int    n,                    // list length
              j,
              L,                    // loop control
              a[MAX_LIST_LEN];   // array to be sorted
      int count = 0;
       for (L = 2){
       while (L <= n)
         j = L;
         while (j>=2
         )a[j]<a[j-1];
            a [j] == a[j-1];
            j = j-1;
       while (L>=1&&++count&&a[j]<a[j-1]
     )cout << count << " ";
    
       return(0);
    }
    }
    When I try to compile I receive these errors:

    Code:
    InsertionSort.cpp:22: error: expected `;' before ) token
    InsertionSort.cpp:34: error: expected primary-expression before } token
    InsertionSort.cpp:34: error: expected `;' before } token
    InsertionSort.cpp:34: error: expected primary-expression before } token
    InsertionSort.cpp:34: error: expected `)' before } token
    InsertionSort.cpp:34: error: expected primary-expression before } token
    InsertionSort.cpp:34: error: expected `;' before } token

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > for (L = 2)
    Read your book again on the structure of for loops.

    And I didn't mean you should blindly delete all declarations of L.
    Renaming them to being loop, index, foo, bar, baz (or any other meaningful name) would perhaps have been more appropriate.

    You need to think a bit about what you're trying to do here. Your program is just a mash of syntax at the moment. Even when you get it to compile, it's not likely to be doing what you want it to.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-26-2010, 08:02 AM
  2. Replies: 5
    Last Post: 08-02-2008, 06:23 AM
  3. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  4. Insertion Sort on Array of Structs
    By n0r3gr3tz in forum C Programming
    Replies: 3
    Last Post: 04-01-2008, 08:28 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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