Функция для вставки сортировки а не сортировки элементов массива
Вот код для сортировки вставки, который не сортирует элементы массива, которые я ввожу..
Помогите, пожалуйста!!
#include <iostream> # void insertion() { int j = 0, i=0,temp,A[12],N; std::cout<<"Enter the no of elements"; std::cin>>N; std::cout<<"Enter the array elements"; std::cin>>A[i]; for(i=1;i<N-1;i++) { i = j - 1; temp = A[i]; while (temp > A[j] && j >= 0) { A[j + 1] = A[j]; j--; } j++; A[j] = temp; } j--; std::cout<<A[temp]; } int main() { insertion(); return 0; } What I have tried: Swapping the positon of main and the function
phil.o
Put a breakpoint at the beginning of your insertion function, then launch a debug session and watch, line by line, the content of your variables. You will be able to spot several issues. Debugging is far from optional for a developer, it is one of the core skills to apply and master. Moreover, you may even find it quite funny actually, because you will be able to see exactly what is going on, instead of randomly moving your functions around and hope for the problem to disappear. The placement of functions relative to each other is not relevant in C/C++; whether you define the main function before or after the insertion function will not affect the final result.