### Reverse the order of an insertion sort?

We have to write a code to randomly fill an array, then ask for an element, then sort the array specifically with an insertion sort, then cout them in ascending order. Buuuuuuut mine is descending. What did I do opposite?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` `````` #include #include #include #include #include using namespace std; int arrray[20]; int search(int arrray[20],int element); void fill(); void display(); void sort(int arrray[]); int main() { int window; int num; int result; fill(); display(); cout << "\n" << endl; cout << "What element are you searching for\?" << endl; cin >> num; result = search(arrray,num); if (result != -1) cout << "\nThe element was found at index " << result << "." << endl; else cout << "\nThe number was not found." << endl; sort(arrray); display(); cin >> window; return 0; } void fill() { srand(1); for(int i = 0; i < 20; i++) { arrray[i] = rand() % 100; } } int search(int arrray[],int element) { int index; for(index = 0; index < 10; index++) { if (element == arrray[index]) break; } if (element == arrray[index]) return index; else return -1; } void display() { int num; int result; for(int x = 0; x < 20; x++) cout << arrray[x] << " "; } void sort(int arrray[]) { int i, j, key, numLength = 20; for(j = 1; j < numLength; j++) { key = arrray[j]; for(i = j - 1; (i >= 0) && (arrray[i] < key); i--) { arrray[i+1] = arrray[i]; } arrray[i+1] = key; } return; }``````
`arrray[i] < key` Try >
@keskiverto

Perfect! Thanks a bunch man!
Topic archived. No new replies allowed.