Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to implement bubble sorting algorithm
- #include <iostream>
- #include<vector>
- #include<sstream>
- using namespace std;
- vector<int> bubbleSort(vector<int>);
- int main()
- {
- int n;
- string str;
- vector<int> ints;
- cout<<"Enter the array to be sorted : ";
- cin>>str;
- for (string::iterator it = str.begin(); it != str.end(); ++it) {
- if (*it == ',') {
- *it = ' ';
- }
- else continue;
- }
- stringstream ss(str);
- while(ss >> n){
- ints.push_back(n);
- }
- ints = bubbleSort(ints);
- cout<<"\nThe Array after sorting is : ";
- for(int i=0; i<ints.size(); i++){
- cout<<ints[i]<<" ";
- }
- }
- vector<int> bubbleSort(vector<int> numbers){
- int temp;
- for(int i=0; i<numbers.size()-1; i++){
- for(int j=0; j< numbers.size()-1; j++){
- if(numbers[j] > numbers[j+1]){
- temp = numbers[j];
- numbers[j] = numbers[j+1];
- numbers[j+1] = temp;
- }
- }
- }
- return numbers;
- }
Add Comment
Please, Sign In to add comment