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

Programming

The document discusses three different sorting algorithms: sequential search, binary search, and bubble sort. Sequential search iterates through an array linearly to find a target value. Binary search divides the array space in half on each step to hone in on the target value. Bubble sort iterates through the array and swaps adjacent elements that are out of order until the array is fully sorted.

Uploaded by

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

Programming

The document discusses three different sorting algorithms: sequential search, binary search, and bubble sort. Sequential search iterates through an array linearly to find a target value. Binary search divides the array space in half on each step to hone in on the target value. Bubble sort iterates through the array and swaps adjacent elements that are out of order until the array is fully sorted.

Uploaded by

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

SEQUENTIAL

#include <iostream>
using namespace std;
//sequential
int main()
{
int size[5]={12, 23, 45, 67, 100};
int target;
int d=1;
cout<<"enter the number your looking for: ";
cin>>target;
for(int i=0; i<5; i++)
{
if(target == size[i])
{
break;
}
d++;
}
if(d >= 1)
{
cout<<"you found: "<<target<<" on index: "<<d<<endl;
}
else
{
cout<<"the number you input is not in the array";
}
return 0;
}
FLOWCHART OF SEQUENTIAL
START

int size[5]={12, 23, 45,


67, 100};
int target;
int d=1;

enter the number your


looking for:

target

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

true
END break target==size[i]

false

d++

true cout<<"you found:


d >= 1 "<<target<<" on index:
"<<d<<endl;

false

cout<<"the number you input is


not in the array";"<<d<<endl;
BINARY
#include <iostream>
using namespace std;
//Binary
int main()
{
int size[9]={12, 23, 45, 50, 68, 79, 88, 99, 202};
int low=0, high=9, midpoint, target;
cout<<"enter the number your looking for: ";
cin>>target;
while (high>=low)
{
midpoint=(low+high)/2;
if (target == size[midpoint])
{
cout<<"found on number: "<<midpoint;
break;
}
else if(target < size[midpoint])
{
high = midpoint-1;
}
else if (target > size[midpoint])
{
low = midpoint+1;
}
}
if (low >high)
{
cout<<"Not found";
}
return 0;
}
FLOWCHART FOR BINARY
START

int size[9]={12, 23, 45, 50, 68, 79,


88, 99, 202};
int low=0, high=9, midpoint, target;

cout<<"enter the number your looking for: ";

cin>>target;

while
(high>=low)

midpoint=(low+high)/2;

TREU
if (target == cout<<"found on
size[midpoint]) number: "<<midpoint;

FALSE
break;

else if(target
high = midpoint-1;
<size[midpoint])
TREU
END

FALSE

TREU
else if (target >
low = midpoint+1;
size[midpoint])

FALSE

if (low >high) cout<<"Not found";


DOBBLE

#include <iostream>

using namespace std;

//SHORTING

void bubbleSort(int arr[], int n) {

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

for (int j = 0; j < n - i - 1; ++j) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

int main() {

int size=5;

int *arr = new int[size];

cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; ++i) {

cin >> arr[i];

bubbleSort(arr, size);

cout << "Sorted array: ";

for (int i = 0; i < size; ++i) {

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

cout << endl;

delete[] arr;

return 0;

}
DOBBLE FOWCHART
START

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


for (int j = 0; j < n - i - 1; ++j)
if (arr[j] > arr[j + 1])

int temp = arr[j];


arr[j] = arr[j + 1];
arr[j + 1] = temp;

int size=5;
int *arr = new int[size];

cout << "Enter " << size <<


" elements: ";

endl
cin >> arr[i];

bubbleSort(arr, size);

cout << "Sorted array: ";

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

false

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

true

END
SELECTION

#include <iostream>

using namespace std;

//Selection shorting

int main()

int sorting[]={3, 1, 4, 2, 5, 7, 9, 6, 8, 10};

int length = 10;

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

cout<<sorting[i]<<", ";

cout<<endl<<endl;

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

int small = i;

for(int b= i+1; b<length; b++)

if (sorting[b] < sorting[small])

small = b;

if(small != i)

int palit = sorting[i];

sorting[i] = sorting[small];

sorting[small] = palit;

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

cout<<sorting[i]<<", ";

return 0;

}
DOBLE FLOWCHART

You might also like