35657

Untitled

Dec 16th, 2023
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. void insert_sort(int arr[], int size) {
  7.     for (int i = 1; i < size; i++) {
  8.         int x = arr[i];
  9.         int k = i - 1;
  10.         while (k >= 0 && arr[k] > x) {
  11.             arr[k + 1] = arr[k];
  12.             arr[k] = x;
  13.             k--;
  14.         }
  15.     }
  16. }
  17.  
  18.  
  19.  
  20. void display(int array[], int size) {
  21.     for (int i = 0; i < size; i++) {
  22.         cout << array[i] << " ";
  23.     }
  24.     cout << endl;
  25. }
  26.  
  27.  
  28. int main() {
  29.     int arr[]{ 1, 3, 7, -4, -2, 4 };
  30.     int size = 6;
  31.     cout << "Original int Array : ";
  32.     display(arr, size);
  33.     insert_sort(arr, size);
  34.     cout << "Sorted   int Array : ";
  35.     display(arr, size);
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment