1. Bubble Sort.
Code:
#include <iostream>
using namespace std;
int main()
int n, i, arr[100], j, temp;
cout << "Enter the Size of Array: ";
cin >> n;
cout << "Enter " << n << " Elements: " << endl;
for (i = 0; i < n; i++)
cin >> arr[i];
for (i = 0; i < (n - 1); i++)
for (j = 0; j < (n - i - 1); j++)
if (arr[j] > arr[j + 1])
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
cout << "\nArray Sorted Successfully!\n";
cout << "\nThe New Sorted Array is: \n";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
2. Selection Sort.
Code:
#include <iostream>
using namespace std;
int main()
int total, arr[100], i, j, temp, small, check, index;
cout << "Enter the Size of Array: ";
cin >> total;
cout << "Enter " << total << " Array Elements: " << endl;
for (i = 0; i < total; i++)
cin >> arr[i];
for (i = 0; i < (total - 1); i++)
check = 0;
small = arr[i];
for (j = (i + 1); j < total; j++)
if (small > arr[j])
small = arr[j];
check++;
index = j;
if (check != 0)
temp = arr[i];
arr[i] = small;
arr[index] = temp;
}
cout << "\nThe Sorted Array is:\n";
for (i = 0; i < total; i++)
cout << arr[i] << " ";
cout << endl;
return 0;
}
3. Insertion Sort.
Code:
#include <iostream>
using namespace std;
int main()
int arr[100], total, i, j, k, element, index;
cout << "Enter the Size of Array: ";
cin >> total;
cout << "Enter " << total << " Array Elements: " << endl;
for (i = 0; i < total; i++)
cin >> arr[i];
for (i = 1; i < total; i++)
element = arr[i];
if (element < arr[i - 1])
for (j = 0; j <= i; j++)
if (element < arr[j])
index = j;
for (k = i; k > j; k--)
arr[k] = arr[k - 1];
break;
else
continue;
arr[index] = element;
cout << "\nThe New Sorted Array is:\n";
for (i = 0; i < total; i++)
cout << arr[i] << " ";
cout << endl;
return 0;