Advertisement
Guest User

Shorting An Array in C++

a guest
Jun 25th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. //Shorting An Array in C++
  2.  
  3. #include<iostream>
  4. #include<cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     int n;
  11.     cout<<"Enter the value of n: ";
  12.     cin>>n;
  13.     int A[n+1];
  14.  
  15.     //Giving value of this array
  16.     cout<<"Enter the value of array: ";
  17.     for(int i=0; i<n; i++){
  18.         cin>>A[i];
  19.     }
  20.  
  21.     //Shorting by swap function
  22.     for(int i=0; i<n-1; i++){
  23.         for(int j =0; j<n-i-1; j++){
  24.             if(A[j]>A[j+1]){
  25.                 swap(A[j],A[j+1]);
  26.             }
  27.  
  28.         }
  29.     }
  30.  
  31.     //output the shorted array
  32.     cout<<"The shorted array is: ";
  33.     for(int i=0; i<n; i++){
  34.         cout<<A[i]<<" ";
  35.     }
  36.  
  37.     //Just for a new line
  38.     cout<<endl;
  39.  
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement