Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void insert_sort(int arr[], int size) {
- for (int i = 1; i < size; i++) {
- int x = arr[i];
- int k = i - 1;
- while (k >= 0 && arr[k] > x) {
- arr[k + 1] = arr[k];
- arr[k] = x;
- k--;
- }
- }
- }
- void display(int array[], int size) {
- for (int i = 0; i < size; i++) {
- cout << array[i] << " ";
- }
- cout << endl;
- }
- int main() {
- int arr[]{ 1, 3, 7, -4, -2, 4 };
- int size = 6;
- cout << "Original int Array : ";
- display(arr, size);
- insert_sort(arr, size);
- cout << "Sorted int Array : ";
- display(arr, size);
- }
Advertisement
Add Comment
Please, Sign In to add comment