Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main(){
- int n, temp, j=0; //additional vars
- bool swapped = true;
- std::cin >> n; //inputing the index of array
- int arr[n];
- for(int i=0;i<n;i++){
- std::cin >> arr[i];
- }
- while(swapped){ //do this loop until everything is swapped in it's order
- swapped = false;
- j++;
- for(int i=0;i<n-j;i++){ //n-j is important if we don't want to loop the same number twice
- if(arr[i]>arr[i+1]){
- temp = arr[i];
- arr[i] = arr[i+1];
- arr[i+1] = temp;
- swapped = true;
- }
- }
- }
- for(int i=0;i<n;i++){ //let's check if everything's in order
- std::cout<<arr[i]<<" ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement