0% found this document useful (0 votes)
44 views4 pages

DSA Lab Task 4

The document contains code snippets demonstrating selection sort and insertion sort algorithms. It includes code to input an array, sort the array using each algorithm, and output the sorted array. The selection sort code compares adjacent elements and swaps them if out of order, while the insertion sort code inserts elements into the sorted portion of the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

DSA Lab Task 4

The document contains code snippets demonstrating selection sort and insertion sort algorithms. It includes code to input an array, sort the array using each algorithm, and output the sorted array. The selection sort code compares adjacent elements and swaps them if out of order, while the insertion sort code inserts elements into the sorted portion of the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

National University of Modern Languages

Department of Software Engineering

Name: Abdul Rehman


Sys Id: Numl-S22-24338
Class: BSSE 31-2A
Instructor: Dr. Javveria Kanwal
Selection Sorting:
#include<iostream>
using namespace std;
void selection_sort(int arr[], int size);
int main()
{
int size;
cout<<"ENTER THE SIZE OF AN ARRAY "<<endl;
cin>>size;
int arr[size];
for(int i=0 ; i<=size-1 ;i++)
{
cout<<"ENTER THE VALUE AT INDEX "<<i<<endl;
cin>>arr[i];
}
selection_sort(arr , size);
return 0;
}
void selection_sort(int arr[] , int size)
{
for(int i=0 ; i<=size-1 ; i++)
{
for(int j=i+1 ; j<size ; j++)
{
if (arr[i]>arr[j])
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
for(int i=0 ; i<=size-1 ;i++)
{
cout<<endl<<"VALUE AT INDEX "<<i<<" -------> ";
cout<<arr[i]<<endl;
}
}

Insertion Sorting:
#include<iostream>
using namespace std;
void insertion_sorting(int arr[] , int size);
int main()
{
int size=7;
int arr[size]={22,33,44,55,66,77,11};
insertion_sorting(arr , size);
}
void insertion_sorting(int arr[] , int size)
{
for(int i=1 ; i<size ; i++)
{
int key = arr[i];
int j=i-1;
while(key<arr[j] && j>=0)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
for(int i=0 ; i<size-1 ; i++){
cout<<"VALUE AT INDEX "<<i<<" is ";
cout<<arr[i]<<endl;

}
}

You might also like