Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Shorting An Array in C++
- #include<iostream>
- #include<cstdlib>
- using namespace std;
- int main()
- {
- int n;
- cout<<"Enter the value of n: ";
- cin>>n;
- int A[n+1];
- //Giving value of this array
- cout<<"Enter the value of array: ";
- for(int i=0; i<n; i++){
- cin>>A[i];
- }
- //Shorting by swap function
- for(int i=0; i<n-1; i++){
- for(int j =0; j<n-i-1; j++){
- if(A[j]>A[j+1]){
- swap(A[j],A[j+1]);
- }
- }
- }
- //output the shorted array
- cout<<"The shorted array is: ";
- for(int i=0; i<n; i++){
- cout<<A[i]<<" ";
- }
- //Just for a new line
- cout<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement