0% found this document useful (0 votes)
21 views

DSA Lab5

Uploaded by

raonetflix1999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

DSA Lab5

Uploaded by

raonetflix1999
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Rao Humza Majeed

Roll no: 02-132212-010

1) Create a program that take an array of 10 inputs from the user and
generate the sorted output using the Insertion Sort Algorithms.

#include <iostream>

using namespace std;

void printArray(int arr[], int n)

for(int i=0; i<n; i++)

cout<<arr[i]<<" ";

cout<<endl;

void insertionSort(int array[], int size) {

for (int step = 1; step < size; step++) {

int key = array[step];

int j = step - 1;

while (key < array[j] && j >= 0) {

array[j + 1] = array[j];

--j;

array[j + 1] = key;
}

int main()

int arr[10];

cout<<"Enter 10 Elements of Array"<<endl;

for(int i=0; i<10; i++)

cin>>arr[i];

int N = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, N);

cout<<"After Array Sorting: "<<endl;

printArray(arr, N);

return 0;

}
2) Take 10 inputs from the user and assign them into two arrays (make 2
arrays of 5 lengths each), merge those arrays and obtain the result in
the sorted manner by using insertion sort.

#include<iostream>

using namespace std;

void printArray(int arr[], int n)

for(int i=0; i<n; i++)

cout<<arr[i]<<" ";

cout<<endl;

void insertionSort(int array[], int size) {

for (int step = 1; step < size; step++) {

int key = array[step];

int j = step - 1;

while (key < array[j] && j >= 0) {

array[j + 1] = array[j];

--j;

array[j + 1] = key;
}

int main()

int a[5], b[5], arr[10];

int i, k;

cout<<"Elements for First Array: "<<endl;

for(i=0; i<5; i++)

cin>>a[i];

arr[i] = a[i];

k = i;

cout<<"Elements for Second Array: "<<endl;

for(i=0; i<5; i++)

cin>>b[i];

arr[k] = b[i];

k++;

cout<<endl;

cout<<"Merged Array: "<<endl;

for(i=0; i<k; i++)

cout<<arr[i]<<" ";
}

cout<<endl;

int N = sizeof(arr) / sizeof(arr[0]);

insertionSort(arr, N);

cout<<endl;

cout<<"After Array Sorting: "<<endl;

printArray(arr, N);

return 0;

You might also like