0% found this document useful (0 votes)
25 views3 pages

Dsa Assign 9

The document describes bubble sort and selection sort algorithms for sorting arrays. It includes C++ code to implement bubble sort and selection sort functions and test them by sorting sample arrays.

Uploaded by

Riaz Malik
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)
25 views3 pages

Dsa Assign 9

The document describes bubble sort and selection sort algorithms for sorting arrays. It includes C++ code to implement bubble sort and selection sort functions and test them by sorting sample arrays.

Uploaded by

Riaz Malik
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/ 3

Q1:-

#include <iostream>
#define MAX 5
using namespace std;
//////////////////////
void BUBBLE_SORT(int A[], int N){
int round, i, temp;
for(int round = 0; round < N; round++){
for(i = 0; i <= (N-1-round); i++){
if(A[i] > A[i+1]){
temp = A[i];
A[i] = A[i+1];
A[i+1] = temp;
}
}
}
}
//////////////////////
int main(){
int A[]={5, 8, 3, 2, 9};
int K;

for(K = 0; K < MAX; K++){


cout<<A[K]<<"\t";
}
cout<<endl;

BUBBLE_SORT(A, MAX);

for(K = 0; K < MAX; K++){


cout<<A[K]<<"\t";
}
cout<<endl;

cout<<endl;
return 0;
}
Q2:-

#include <iostream>
using namespace std;
//////////////////////
int MIN(int A[], int K, int N){
int j, LOC, MIN;
MIN = A[K];
LOC = K;
for(j = K; j <= N-1; j++){
if(MIN > A[j]){
MIN = A[j];
LOC = j;
}
}
return LOC;
}
/////////////////////
void SELECTION_SORT(int A[], int N){
int LOC, K, temp;
for(K = 0; K < N; K++){
LOC = MIN(A, K, N);
temp = A[K];
A[K] = A[LOC];
A[LOC] = temp;
}
}
////////////////////
int main(){
int A[]={5, 8, 3, 2, 9};
int K;

for(K = 0; K < 5; K++){


cout<<A[K]<<"\t";
}
cout<<endl;

SELECTION_SORT(A, 5);
for(K = 0; K < 5; K++){
cout<<A[K]<<"\t";
}
cout<<endl;

cout<<endl;
return 0;
}

You might also like