0% found this document useful (0 votes)
70 views8 pages

Flowchart

The document contains flowcharts and code for bubble sort, selection sort, sequential search, and binary search algorithms. It shows the step-by-step process of each algorithm through visual flowcharts and corresponding C++ code implementations.

Uploaded by

ron02naadat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views8 pages

Flowchart

The document contains flowcharts and code for bubble sort, selection sort, sequential search, and binary search algorithms. It shows the step-by-step process of each algorithm through visual flowcharts and corresponding C++ code implementations.

Uploaded by

ron02naadat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

START Bubble Sorting Flowchart

int size = 5;
int array[size] = {5, 1, 3, 2, 4};
bool trade;

while
false int i = 0;
(trade = true)

i<
true

i++
trade = false;

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

int i = 1;

false
i<
END
i
//output 1, 2, 3, 4, 5
true

++i

If
(arr[i - 1]
false
true

int temp = arr[i];


arr[i] = arr[i - 1];
arr[i - 1] = temp;

trade = true;
``cpp Bubble Sort
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int size){
bool trade = true;
while(trade){
trade = false;
for(int i = 1; i < size; ++i){
if (arr[i - 1] > arr[i]){
int temp = arr[i];
arr[i] = arr[i - 1];
arr[i - 1] = temp;
trade = true;
}
}
};
}
int main ()
{
int size = 5;
int array[size] = {5, 1, 3, 2, 4};
bubbleSort(array,size);
for(int i = 0; i < size; i++)
{
cout<<array[i]<<", ";
}
return 0;
}
START
Selection Sorting Flowchart

int n = 5; int temp = arr[i];


int array[n] = {9, 10, 4, 1, 6}; arr[i] = arr[minimum];
arr[minimum] = temp;

int i = 0;
int i = 0;

i<n i<n

i++
i++

int minimum = i;

cout<<array[i]<<", ";
int j = i + 1;

j<n
false END

true //output 1, 4, 6, 9, 10

j++

false

if (arr[j] <
arr[minimum])

minimum = j;
``cpp Selection Sort
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n){
for(int i = 0; i < n - 1; i++)
{
int minimum = i;
for(int j = i + 1; j < n; j++){
if(arr[j] < arr[minimum]){
minimum = j;
}
}
int temp = arr[i];
arr[i] = arr[minimum];
arr[minimum] = temp;
}
}
int main()
{
int n = 5;
int array[n] = {9, 10, 4, 1, 6};
selectionSort(array,n);
for(int i = 0; i < n; i++){
cout<<array[i]<<", ";
}
return 0;
}
START Sequential Search Flowchart
cout
<<"The target found in
index: "<<result<<endl;

int size = 10;


int array[size] =
{2,4,6,8,10,12,14,16,18,20};
int target = 20;

END

int i = 0; //output:
The target found in index: 9

i<
size

i++

false
If
(arr[i] == target)

true

return i;

//i is the return value


int result = i;

If true
cout<<"The target isn't
(result != false
found in the set :( ";
``cpp Sequential Search
#include <iostream>
using namespace std;
int sequentialSearch(int arr[],int size, int target)
{
for(int i = 0; i < size; i++){
if(arr[i] == target){
return i;
}
}
}
int main()
{
int size = 10;
int array[size] = {2,4,6,8,10,12,14,16,18,20};
int target = 20;
int result = sequentialSearch(array,size,target);
if(result != -1){
cout<<"The target found in index: "<<result<<endl;
}
else{
cout<<"The target isn't found in the set :( ";
}
return 0;
}
START
Binary Search Flowchart
//mid is the return value

int index = mid;


int size = 10;
int array[size] =
{10, 21, 45, 65, 1, 3, 5, 26, 18, 69};
int target = 69;
If false
(index !=
1)
int left = 0;
int right = size - 1;
true else

return

false
while
cout
(left <= right)
<<"The target is found
in index:
"<<index<<endl;
true

int mid = (left + right) /


cout
2 <<"The target isn't
return found in the set :( ";
mid;

true
if
(arr[mid] == target)

false
left = mid + 1; END

true //output:

else if The target is found in index: 9


(arr[mid] < target)

right = mid -1;


false

true
else

false
``cpp Binary Search
#include <iostream>
using namespace std;
int binarySearching(int arr[], int size, int target)
{
int left = 0;
int right = size - 1;

while(left <= right){


int mid = (left + right) / 2;
if(arr[mid] == target){
return mid;
}
else if(arr[mid] < target){
left = mid + 1;
}
else{
right = mid - 1;
}
}
return -1;
}
int main()
{
int size = 10;
int array[size] = {10, 21, 45, 65, 1, 3, 5, 26, 18, 69};
int target = 69;
int index = binarySearching(array,size,target);
if(index != -1){
cout<<"The target is found in index: "<<index<<endl;
}
else
{
cout<<"The target isn't found in the set :( ";
}
return 0;
}

You might also like