Monday, October 12, 2015

algorithm: Bubble sort

Bubble sort:

Scan from bottom to top, put lighter element up.

/////////////////////
class Solution {
public:
       vector<int> BubbleSort(vector<int> s)
       {
           //implement the bubble sort
              int sLen=s.size();
              for (int i = 0; i < sLen; i++){
                     for (int j = sLen - 1; j > i; j--){
                           if (s[j] < s[j - 1]) swap(s[j],s[j-1]);
                     }
              }
              return s;
       }
};

No comments:

Post a Comment