├── 2. Selection sort algorithm using c++ ├── Bubble sort algorithm in datastructures using c++ ├── Insertion sort algorithm in datastructures using c++ └── Merge sort algorithm in data structures using c++ /2. Selection sort algorithm using c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swapping(int &a, int &b) 4 | { 5 | int temp; temp = a; a = b; 6 | b = temp; 7 | } 8 | void display(int *array, int size) 9 | { for(int i = 0; i> n; 31 | int arr[n]; 32 | cout << "Enter elements:" << endl; 33 | for(int i = 0; i> arr[i]; 36 | } 37 | cout << "Array before Sorting: "; 38 | display(arr, n); 39 | selectionSort(arr, n); 40 | cout << "Array after Sorting: "; 41 | display(arr, n); 42 | } 43 | -------------------------------------------------------------------------------- /Bubble sort algorithm in datastructures using c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void BubbleSort (int arr[], int n) 4 | { 5 | int i, j; 6 | for (i = 0; i < n; ++i) 7 | { 8 | for (j = 0; j < n-i-1; ++j) 9 | { 10 | if (arr[j] > arr[j+1]) 11 | { 12 | arr[j] = arr[j]+arr[j+1]; 13 | arr[j+1] = arr[j]-arr[j + 1]; 14 | arr[j] = arr[j]-arr[j + 1]; 15 | } 16 | } 17 | } 18 | } 19 | int main() 20 | { 21 | int n, i; 22 | cout<<"\nEnter the number of data element to be sorted: "; cin>>n; 23 | int arr[n]; 24 | for(i = 0; i < n; i++) 25 | { 26 | cout<<"Enter element "<>arr[i]; 27 | } 28 | BubbleSort(arr, n); cout<<"\nSorted Data "; for (i = 0; i < n; i++) cout<<"->"< 2 | void insertionSort(int arr[], int n) 3 | { 4 | int i, key, j; 5 | for (i = 1; i < n; i++) 6 | { 7 | key = arr[i]; j = i - 1; 8 | 9 | while (j >= 0 && arr[j] > key) 10 | { 11 | arr[j + 1] = arr[j]; j = j - 1; 12 | } 13 | arr[j + 1] = key; 14 | } 15 | } 16 | void printArray(int arr[], int n) 17 | { 18 | int i; 19 | for (i = 0; i < n; i++) cout << arr[i] << " "; 20 | cout << endl; 21 | } 22 | int main() 23 | { 24 | int arr[] = { 12, 11, 13, 5, 6 }; 25 | int n = sizeof(arr) / sizeof(arr[0]); 26 | insertionSort(arr, n); printArray(arr, n); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /Merge sort algorithm in data structures using c++: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void swapping(int &a, int &b) 4 | { 5 | int temp; temp = a; a = b; 6 | b = temp; 7 | } 8 | void display(int *array, int size) 9 | { 10 | for(int i = 0; i> n; 52 | int arr[n]; 53 | cout << "Enter elements:" << endl; for(int i = 0; i> arr[i]; 55 | cout << "Array before Sorting: "; display(arr, n); 56 | 57 | mergeSort(arr, 0, n-1); 58 | cout << "Array after Sorting: "; display(arr, n); 59 | } 60 | 61 | --------------------------------------------------------------------------------