Skip to content

Commit

Permalink
Merge pull request #51 from its-amans/main
Browse files Browse the repository at this point in the history
insertion_sort
  • Loading branch information
lilmistake authored Oct 20, 2023
2 parents f9d198f + 6b67ebf commit 76ea15c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions DSA_Codesheet/C/insertion_sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<stdio.h>
int main(){
int n;
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter the unsorted array:");
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(int i=1;i<n;i++){
int current =arr[i];
int j=i-1;
while(arr[j]>current &&j>=0){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
printf("sorted list:\n");
for(int i=0;i<n;i++){
printf("%d\n",arr[i]);
}
return 0;
}
25 changes: 25 additions & 0 deletions DSA_Codesheet/Cpp/insertion_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Enter the size of array:";
cin>>n;
cout<<"Enter the unsorted array:"<<endl;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
for(int i=1;i<n;i++){
int current =arr[i];
int j=i-1;
while(arr[j]>current &&j>=0){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=current;
}
cout<<"sorted list:";
for(int i=0;i<n;i++){
cout<<arr[i]<<" ";
}cout<<endl;
}

0 comments on commit 76ea15c

Please sign in to comment.