Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<vector>
- #include<sstream>
- using namespace std;
- vector<int> shellSort(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 = shellSort(ints);
- cout<<"\nThe Array after sorting is : ";
- for(int i=0; i<ints.size(); i++){
- cout<<ints[i]<<" ";
- }
- }
- vector<int> shellSort(vector<int> numbers){
- int gap = numbers.size() / 2;
- while (gap > 0){
- // loop till i is in range from gaps to lenght of the list
- for (int i = 0; i < numbers.size(); i++){
- // store the ith element in the temp variable
- int temp = numbers[i];
- int j = i;
- // applying insertion sort on the sublist
- // shifting the elements larger than the ith element to the right
- while(j >= gap && numbers[j - gap] > temp){
- numbers[j] = numbers[j - gap];
- j -= gap;
- }
- // puting the element at its correct position
- numbers[j] = temp;
- }
- // reduce gap by it half
- gap = gap/2;
- }
- return numbers;
- }
Add Comment
Please, Sign In to add comment