Why is my insertion sort code not working for certain array sizes?

  • C/C++
  • Thread starter waver.
  • Start date
  • Tags
    C++ Sort
In summary, when using insertion sort, it is important to pay attention to the indexing and make sure it is consistent throughout the code. In this case, mixing 0-based and 1-based indexing caused an off-by-one error, resulting in incorrect sorting and errors. To locate the mistake, it is recommended to use a debugger or sprinkle print statements to check the values of variables at key points in the code. Additionally, changing the second (or third) for loop to use < instead of <= can help prevent indexing beyond the end of the array.
  • #1
waver.
8
0
hi Guys
i wrote a insertion sort code which is
Code:
#include <iostream> /*Waver*/
using namespace std;
int main()

{
int n,i,m;
float a[100],k;

cout<<"enter the amount of number";
cin>>n;

for(i=0; i<n; i++)
cin>>a[i];

for(i=1; i<=n; i++)
{
    for(m=1 ; m<=i; m++)
    {
      if(a[m]<a[m-1])
      {
          k=a[m-1];
          a[m-1]=a[m];
          a[m]=k;
      }

    }
}
for(i=0; i<n; i++)
cout<<a[i] <<"  ";
return 0;
}
it work fine with some size used in array and won't work with other size
i mean when i enter n=5 it work fine when i enter n=4 it won't work and sort is wrong

when n=5

fy3fnp.jpg
and when n=4
the sort is wrong and error appear with last number
s5l6jl.jpg


i just want to know what i did wrong . thanks in advance
 
Technology news on Phys.org
  • #2
Hint: You are mixing 0-based and 1-based indexing in your logic. This is not a good idea. Read about off-by-one errors, and go from there.
 
  • #3
I would use your debugger and step through the code making sure you agree with what values the computer has set. Doing this you should be able to locate your mistake.

If you think that's too difficult then sprinkle print statements after key lines to see values of indexes and other key variables.
 
  • #4
jedishrfu said:
I would use your debugger and step through the code making sure you agree with what values the computer has set. Doing this you should be able to locate your mistake.

If you think that's too difficult then sprinkle print statements after key lines to see values of indexes and other key variables.

the debugger didnt cought any thing
14vc6h.jpg
 
  • #5
Change the second (or the third) for loop to use < instead of <=. You're indexing beyond the end of the array.
 
  • #6
When I said use the debugger to step through your code, I didn't mean run your code in the debugger.

The debugger can literally step one line at a time and as you do that you can inspect the values of any variable to see if they are what you expect. Many times you will see your error right away.

Or in the case of @rcgldr's hint you will discover that you looped one too many times.
 

1. How does the insertion sort algorithm work in C++?

The insertion sort algorithm works by dividing the given array into two sub-arrays: a sorted sub-array and an unsorted sub-array. The sorted sub-array starts with the first element of the given array and gradually increases as elements are sorted. The unsorted sub-array contains the remaining elements that need to be sorted. The algorithm then iterates through the unsorted sub-array, comparing each element to the elements in the sorted sub-array and inserting it at the correct position. This process is repeated until all elements in the unsorted sub-array are sorted and the entire array is in ascending order.

2. Why is insertion sort often preferred over other sorting algorithms in C++?

Insertion sort has a relatively simple implementation and requires minimal additional space, making it a good choice for smaller arrays or data sets. It also has a time complexity of O(n^2), which is comparable to other simple sorting algorithms. In some cases, insertion sort can even outperform other sorting algorithms, such as when the array is nearly sorted or contains mostly already sorted elements.

3. How can the efficiency of insertion sort in C++ be improved?

One way to improve the efficiency of insertion sort is by using the binary insertion sort algorithm, which uses a binary search to find the correct position for each element in the sorted sub-array. This reduces the number of comparisons needed, resulting in a time complexity of O(n*log n). Another way to improve efficiency is by implementing the algorithm in a multithreaded or parallel manner, allowing for faster sorting of larger arrays.

4. What are the advantages and disadvantages of using insertion sort in C++?

One advantage of using insertion sort is its simplicity and ease of implementation. It also has a space complexity of O(1), meaning it does not require any additional memory beyond the given array. However, insertion sort can be inefficient for larger arrays or data sets, as its time complexity of O(n^2) can result in longer sorting times compared to other more efficient sorting algorithms.

5. Can insertion sort be used for sorting other data types besides integers in C++?

Yes, insertion sort can be used to sort other data types besides integers in C++. However, it may require some modifications to the comparison function used in the algorithm. For example, if sorting strings, a string comparison function would need to be used instead of the default integer comparison. Additionally, the implementation of insertion sort may need to be adjusted to handle the specific data type being sorted.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
3K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
10
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top