Dsa Practicals
Dsa Practicals
Dsa Practicals
8
Write a program to implement bubble sort
#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n){
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}
void printArray(int arr[], int size){
int i;
for (i = 0; i < size; i++)
cout << arr[i] << " ";
cout << endl;
}
int main(){
int arr[] = {10,15,65,2,3};
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;
}
#include<iostream>
using namespace std;
int main(){
int A[] = {3, 5, 2, 13, 12};
int n = 5;
cout<<"Before sort"<<endl;
printArray(A, n);
selectionSort(A, n);
cout<<"\nAfter sort"<<endl;
printArray(A, n);
return 0;
}
#include<iostream>
using namespace std;
do{
while(A[i] <= pivot){
i++;
}
if(i<j){
temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}while(i<j);
swap(A[low], A[j]);
if(low<high){
partitionIndex = partition(A, low, high);
quickSort(A, low, partitionIndex-1);
quickSort(A, partitionIndex+1, high);
}
}
int main(){
int A[] = {5, 2, 13, 12, 3, 2, 10, 25, 56, 76, 45, 5, 74};
int n = sizeof(A)/sizeof(int);
cout<<"Before sorting\n";
printArray(A, n);
quickSort(A, 0, n-1);
cout<<"\nAfter sorting\n";
printArray(A, n);
return 0;
}