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

DSA Lab Task 3

This document discusses sorting algorithms and provides code examples of sorting arrays in C++. It includes an example of bubble sort to sort an integer array in ascending order. It also provides examples of binary search and sorting algorithms implemented as functions that take an array as a parameter and return the sorted array or search result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

DSA Lab Task 3

This document discusses sorting algorithms and provides code examples of sorting arrays in C++. It includes an example of bubble sort to sort an integer array in ascending order. It also provides examples of binary search and sorting algorithms implemented as functions that take an array as a parameter and return the sorted array or search result.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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
Sorting:
A Sorting Algorithm is used to rearrange a given array or list of elements
according to a comparison operator on the elements. The comparison operator is
used to decide the new order of elements in the respective data structure.
For Example: 
The below list of characters is sorted in increasing order of their ASCII
values. That is, the character with a lesser ASCII value will be placed first than the
character with a higher ASCII value.
Code:
#include<iostream>
using namespace std;
int main()
{
int size;
cout<<"ENTER THE SIZE OF AN ARRAY"<<endl;
cin>>size;

cout<<"------------------------------------------------------------------------------------------
------------------------------"<<endl;
int arr[size];
for (int i=0 ; i<size ; i++)
{
cout<<"ENTER THE VALUE AT INDEX "<<i<<endl;
cin>>arr[i];
}
//-------> CODE FOR SORTING
for(int i = 0 ; i<size-1 ; i++)
{
for (int j=0 ; j<size-1-i ; j++)
{
if (arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr [j+1];
arr[j+1] = temp;
}
}
}
//--------> PRINTING AN ARRAY AFTER SORTING
for (int i=0 ; i<size ; i++)
{
cout<<endl<<arr[i]<<endl;
}
}
Binary search with function:
#include<iostream>
using namespace std;
int binarysearch(int arr[] , int value);
int size;
int main(){
cout<<"ENTER THE SIZE OF AN ARRAY";
cin>>size;
int value;
cout<<"ENTER THE VALUE WHICH YOU WANNA FIND";
cin>>value;
int arr[size];

for (int i=0 ; i<size-1 ; i++)


{
cout<<"ENTER THE VALUE AT INDEX "<<i<<endl;
cin>>arr[i];
}

int result = binarysearch(arr,value);


if (result==1)
{
cout<<"Value found";
}
else
{
cout<<"value not found"<<endl;
}

}
//------>BODY OF A FUNCTION
int binarysearch(int arr[] , int value)
{
int beg=0;
int end=size;
while(beg<=end)
{
int mid=(beg+end)/2;
if(arr[mid]==value)
{
cout<<value<<" is at "<<mid<<endl;
return 1;
}
else if(arr[mid]>value)
{
end = mid-1;
}
else if(arr[mid]<value)
{
beg=mid+1;
}
else
{
cout<<"invalid";
}
}
}
Sorting with function:
#include<iostream>
using namespace std;
int sorting(int arr[]);
int size=10;
int main(){
int arr[10]={1,2,3,4,5,6,7,8,9,10};
cout<<sorting(arr);
}
int sorting(int arr[])
{
for(int i = 0 ; i<size-1 ; i++)
{
for (int j=0 ; j<size-1-i ; j++)
{
if (arr[j]>arr[j+1])
{
int temp = arr[j];
arr[j] = arr [j+1];
arr[j+1] = temp;
}
}
}
//--------> PRINTING AN ARRAY AFTER SORTING
for (int i=0 ; i<size ; i++)
{
cout<<endl<<arr[i]<<endl;
}
}

You might also like