/*Bubble sort*/
#include <iostream>
using namespace std;
//function to sort the array
int bubble_sort (int k[], int n){
int t,ct=1;
while(ct<n){
for(int i=0;i<n-ct;i++){
if(k[i]>=k[i+1]){
t=k[i];
k[i]=k[i+1];
k[i+1]=t;
}
ct++;
}
}
///loop to o/p the sorted array
for(int i=0;i<n;i++){
cout<<k[i]<<" ";
}cout<<endl;
return 0;
}
int main(){
int l;
int r[l];
cout<<"Enter the array size"<<endl;
cin>>l;
cout<<"Enter elements"<<endl;
for(int i=0;i<l;i++){
cin>>r[i];
}
bubble_sort(r,l);
return 0;
}
/*Insertion Sort*/
#include <iostream>
using namespace std;
//function to sort the array
int insertion_sort (int k[], int n){
int t,ct=1;
for(int i=1;i<n;i++){
int current=k[i];
int j=i-1;
while(k[j]>current && j>=0){
k[j+1]=k[j];
j--;
}
k[j+1]=current;
}
for(int i=0;i<n;i++){
cout<<k[i]<<" ";
}cout<<endl;
return 0;
}
int main(){
int l;
int r[l];
cout<<"Enter the array size"<<endl;
cin>>l;
cout<<"Enter elements"<<endl;
for(int i=0;i<l;i++){
cin>>r[i];
}
insertion_sort(r,l);
return 0;
}
The images attached show the output that I get in the terminal(which is blank) when I run the respective codes. The first algorithm illustrates the bubble sorting algorithm and the 2nd is meant to implement insertion sorting technique. Any help in this regard is much appreciated!
Output in the terminal for bubble sort code
Output in the terminal for the insertion sort code