Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- void swap(int* a, int* b);
- int main(){
- int first=0,last=0,j=0,n=0,mark=0,bool=0,arr[15];
- do{
- // entering array
- printf("enter the size of the array(less than 15): ");
- scanf("%d",&n);
- printf("enter the array members: ");
- for(j=0;j<n;j++){
- scanf("%d",&arr[j]);
- }
- printf("\nthe entered array: ");
- for(j=0;j<n;j++){
- printf("%d ",arr[j]);
- }
- // sorting array
- last=(n-1);
- for(j=0,mark=0;mark<last;mark++){
- j=mark+1;
- while(arr[j-1]>arr[j]){
- swap(&arr[j-1],&arr[j]);
- if(j>1) // this step is CRUCIAL, as when j=1, decrementing it will result in arr[-1] to be compared in next while iteration
- j--;
- }
- }
- // printing the sorted array
- printf("\nthe sorted array: ");
- for(j=0;j<n;j++){
- printf("%d ",arr[j]);
- }
- printf("\n\niterate again for new number?\n yes(1) or no(0)\n enter choice: ");
- scanf("%d",&bool);
- }while(bool==1);
- return 0;
- }
- void swap(int* a, int* b){
- *a=*a + *b;
- *b=*a-*b;
- *a=*a-*b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement