// C++ program for selection sort elements from accept from user
#include <iostream>
using namespace std;
int main(){
int n, temp,i,j, smallest;
cout<<" Enter number of Elements n= ";
cin>>n;
int list[n];
cout<<" Enter Elements : ";
for(int i=0;i<n;i++)
cin>>list[i];
for(i=0;i<n;i++){
smallest=i;
for(j=i+1;j<n;j++){
if(list[j]<list[smallest])
smallest=j;
if(smallest!=i){
swap(list[i],list[smallest]);
}}
for(int i=0;i<n;i++) {
cout<<list[i]<<ends;
return 0;
}
// C++ program for implementation of selection sort in the given elements
#include <iostream>
using namespace std;
// Function for Selection sort
void selectionSort(int arr[], int n)
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n - 1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}}
// Swap the found minimum element
// with the first element
if (min_idx != i)
swap(arr[min_idx], arr[i]);
}}
// Function to print an array
void printArray(int arr[], int size)
for (int i = 0; i < size; i++) {
cout << arr[i] << " "<<endl;
}}
// Driver program
int main()
int arr[] = { 64, 25, 12, 22, 11 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
selectionSort(arr, n);
cout << "Sorted array: \n";
printArray(arr, n);
return 0;
}
// C++ program for insertion sort that elements accept from the user
#include <iostream>
using namespace std;
int main(){
int n,j;
//int key;
cout<<" Enter number of Items n= ";
cin>>n;
int list[n];
cout<<" Enter Elements : ";
for(int i=0;i<n;i++)
cin>>list[i];
int temp;
for(int i=1;i<n;i++){
temp=list[i];
j=i-1;
while(j>=0 && list[j]>temp)
list[j+1]=list[j];
j--;
list[j+1]=temp;
for(int i=0;i<n;i++)
cout<<list[i]<<ends;
}
// C++ program for insertion sort for the given elements
#include <bits/stdc++.h>
using namespace std;
// Function to sort an array using
// insertion sort
void insertionSort(int arr[], int n)
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
// Move elements of arr[0..i-1],
// that are greater than key,
// to one position ahead of their
// current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
// A utility function to print an array of size n
void printArray(int arr[], int n)
int i;
for (i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
// Driver code
int main()
int arr[] = { 12, 11, 13, 5, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, N);
printArray(arr, N);
return 0;
}
// C++ program for bubble sort that elements accept from user
#include <iostream>
using namespace std;
int main(){
int n,i,j,temp;
cout<<" Enter number of Items n= ";
cin>>n;
int list[n];
cout<<" Enter Elements : ";
for(int i=0;i<n;i++)
cin>>list[i];
for(i=0;i<n; i++){
for(j=n-1;j>i; j--){
if(list[j]<list[j-1]){
temp=list[j];
list[j]=list[j-1];
list[j-1]=temp;
for(i=0;i<n; i++){
cout<<list[i]<<ends;
}
// optimized implementation of Bubble sort for the given elements
#include <bits/stdc++.h>
using namespace std;
// An optimized version of Bubble Sort
void bubbleSort(int arr[], int n)
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
// If no two elements were swapped
// by inner loop, then break
if (swapped == false)
break;
}}
// Function to print an array
void printArray(int arr[], int size)
int i;
for (i = 0; i < size; i++)
cout << " " << arr[i];
// Driver program to test above functions
int main()
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int N = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, N);
cout << "Sorted array: \n";
printArray(arr, N);
return 0;