Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Apr 2008
    Posts
    257
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Insertion sort C++ help

    hey guys,

    I need to sort an array in ascending order in C++ AS the user inputs the numbers in main,

    for example, in main

    Code:
    for (int a=0;a<10;a++) {
    
    		cout<<"Enter a Number: " << endl;
    		cin >> numEntered;
    		insertArray(array, numEntered, a);
    	}
    can you guys gimme some examples to sort the array AS the user inputs the number after the next number?

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    You would just scan the array comparing the data at the current index and the next index with the new data to find where it goes in the array. Then just insert the data at that location. So you would want to use a for loop.
    OracleGuy


  •  

    Posting Permissions

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