Sign up to create your own snipts, or login.

Public snipts » sort The latest public sort snipts.

showing 1-20 of 37 snipts for sort
  • fruit sort
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    
    using namespace std;
    
    class tropical //Class tropical
    {
        public:
        string name;
        int price;
    };
    
    bool sortByName(const tropical &t1,const tropical &t2) //comparison function to be used compare the names of 2 fruit objects
    {
        return t1.name < t2.name;
    }
    
    bool sortByPrice(const tropical &p1,const tropical &p2) //comparison function to be used to compare the price of 2 fruit objects
    {
        return p1.price < p2.price;
    }
    
    void displayfruits(vector<tropical> fruitlist) //function that displays objects in vector
    {
        cout << "Name \t\tPrice" << endl << "==========\t=====" << endl;
        for(int i=0;i<10;i++)
        {
            cout << fruitlist[i].name << "  \t" << fruitlist[i].price << endl;
        }
        cout << endl;
    }
    
    int main()
    {
        int sortchoice; //variable used to select sorting method
        string searchfruit; //variable used to store input from user to search for fruit
    
        //sample fruit names and prices
        string fruitname[] = {"Avocado", "Papaya", "Grapefruit", "Pineapple", "Jackfruit", "Orange", "Honeydew", "Mangosteen", "Banana", "Durian"};
        int fruitprice[] = {2, 4, 6, 1, 10, 3, 9, 7, 5, 8};
    
        vector<tropical> fruitlist; //vector used to store tropicalfruit class objects
        tropical fruit; //tropicalfruit class object
    
        for(int i=0; i<10; i++) //puts 10 objects with name and price into vector
        {
            fruit.name = fruitname[i];
            fruit.price = fruitprice[i];
    
            fruitlist.push_back(fruit);
        }
    
        displayfruits(fruitlist); //displays objects in fruitlist vector
    
        while(1)
        {
            cout<<"Arrange fruits by 1)Name or 2)Price : "; //asks user whether to sort by name or price
            cin>>sortchoice;
            if(sortchoice == 1)
            {
                sort (fruitlist.begin(), fruitlist.end(), sortByName); //sorts fruit objects by their names
            }
            else if(sortchoice == 2)
            {
                sort (fruitlist.begin(), fruitlist.end(), sortByPrice);//function to sort fruit objects by price
            }
    
            displayfruits(fruitlist); //displays objects in fruitlist vector
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by Rajendran on Jan 29, 2010 at 1:23 p.m. EST
  • Sort Fruit and Search
    #include <iostream>
    #include <list>
    #include <string>
    #include <algorithm>
    using namespace std;
    class Fruits
    {
        public:
        string fruitName;
        double price;
    };
    bool ASCsortByName(Fruits &A, Fruits &B)
    {
        return (A.fruitName < B.fruitName);
    }
    bool DESCsortByName(Fruits &A, Fruits &B)
    {
        return (A.fruitName > B.fruitName);
    }
    
    
    int main()
    {
        Fruits S;
        string look;
      list<Fruits> listA; //declare list object
      list<Fruits>:: iterator it = listA.begin();
      int menu;
      for(int i=0;i<9;i++)
      {
          cout<<"Please enter Fruits Name followed by Price : ";
          cin>>S.fruitName;
          cin>>S.price;
          listA.push_back(S);
      }
        cout<<"Arrange Fruit Name by Ascending(1) or Descending(2)?";
        cin>>menu;
        if(menu==1)
        {
            listA.sort(ASCsortByName);
        }
        else if(menu==2)
        {
            listA.sort(DESCsortByName);
        }
    
          cout<<endl<<"List is sorted by descending : ";
      for(it=listA.begin();it!=listA.end();it++)
      {
          cout<<endl<<(*it).fruitName<<" " <<(*it).price;
      }
    cout<<endl<<"Search for Fruits : ";
      cin>>look;
    
    for(it=listA.begin();it!=listA.end();it++)
    {
        if(look!=(*it).fruitName)
        {
            cout<<"Not Found";
            break;
        }
        else
        {
            cout<<"Found";
        }
        it++;
    }
    }
    

    copy | embed

    0 comments - tagged in  posted by gasnake on Jan 25, 2010 at 3:20 a.m. EST
  • STL sort with own function
    #include <iostream>
    #include <algorithm>    // for sort
    #include <functional>   // for comp function
    #include <iterator>     // for iterator
    using namespace std;
    
    bool myfunction(int i,int j)
    {
        return (i<j);
    }
    
    int main()
    {
        int num[ ] = { -9, 4, 3,-3, 0, -2, 7, 4} ;
        ostream_iterator<int> output(cout, " ");        //only work for array and vectors not for list
    
    
        cout << "Original array: ";
        copy(num,num+8,output);                 //output array using iterator
        //for(int i=0;i<8;i++)
        //cout<<num[i]<<" ";
    
        sort(num,num+8);                        //sort without comp function
        cout <<endl<< "asending order : ";
        copy(num,num+8,output);
    
        sort(num,num+8,greater<int>());         //sort with comp function
        cout <<endl<< "deasending order : ";
        copy(num,num+8,output);
    
        sort(num,num+8,myfunction);         //sort with own function
        cout <<endl<< "asending order : ";
        copy(num,num+8,output);
    
        cout<<endl;
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by saravanansaravanan on Jan 24, 2010 at 10:43 p.m. EST
  • bubble sort decending
    #include <iostream>
    
    using namespace std;
    
    /* bubble sort */
    void bubbleSort(int list[], int arraySize)
    {
        bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
    // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] > list[i + 1])
                {
    // Swap list[i] with list[i + 1]
                    int temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }
    
    int main()
    {
        int array[] = {23, 2, 56, 89, 2, 42, 9, 11, 100, 35, 2, 17};
        int i=0;
        for(i=0;i<12;i++)
        {
        cout<<array[i]<< "  ";
        }
    
        bubbleSort(array,12);
        for(i=0;i<12;i++)
        {
            cout<<array[i]<< "  "<<endl;
        }
    
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by cheechiin on Jan 24, 2010 at 10:35 p.m. EST
  • insertion sort
    #include <iostream>
    using namespace std;
    void insertSort(int a[], int size)
    {
    int i;
    int j;
    int temp;
    
    for (i = 1; i < size; i++)
    //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
    {
    temp = a[i];//the item taken out for insertion, always from the unsorted list
    for (j = i - 1; j >= 0 && a[j] > temp; j--)
    {
    //for the items that are larger then the item taken out to be insert,
    // to make way for item to be inserted
    a[j + 1] = a[j];//move it out into the rhs item
    }
    a[j + 1] = temp;//insert the item
    }
    }
     int main()
    {
        int arrayOne[] = {23, 2, 56, 89, 2, 42, 9, 11, 100, 35, 2, 17};
        insertSort(arrayOne,12);
    
    for(int i=0;i<12;i++)
    {
      cout<<arrayOne[i]<<" ";
    }
    return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by hyde on Jan 24, 2010 at 10:28 p.m. EST
  • sort with function
    #include <iostream>
    #include <algorithm>    // for sort
    #include <functional>   // for comp function
    #include <iterator>     // for iterator
    using namespace std;
    
    int main()
    {
        int num[ ] = { -9, 4, 3,-3, 0, -2, 7, 4} ;//only work for array and vectors not for list
    
        ostream_iterator<int> output(cout, " ");
    
    
        cout << "Original array: ";
        copy(num,num+8,output);                 //output array using iterator
        //for(int i=0;i<8;i++)
        //cout<<num[i]<<" ";
    
        sort(num,num+8);                        //sort without comp function
        cout <<endl<< "asending order : ";
        copy(num,num+8,output);
    
        sort(num,num+8,greater<int>());         //sort with comp function
        cout <<endl<< "deasending order : ";
        copy(num,num+8,output);
    
        sort(num,num+8,less<int>());         //sort with comp function
        cout <<endl<< "asending order : ";
        copy(num,num+8,output);
    
        cout<<endl;
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by saravanansaravanan on Jan 24, 2010 at 10:27 p.m. EST
  • insertion sort int char string
    #include <iostream>
    
    using namespace std;
    
    //shin's insertion sort
    template <typename T>
    void insertSort(T a[], int size)
    {
        int i;
        int j;
        T temp;
    
        for (i = 1; i < size; i++)
    //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
        {
            temp = a[i];//the item taken out for insertion, always from the unsorted list
            for (j = i - 1; j >= 0 && a[j] > temp; j--)
            {
    //for the items that are larger then the item taken out to be insert,
    // to make way for item to be inserted
                a[j + 1] = a[j];//move it out into the rhs item
            }
            a[j + 1] = temp;//insert the item
        }
        //for(int m=0;m<5;m++)
        //cout<<a[m];
    }
    
    int main()
    {
        int a1[] ={9,4,2,8,1};
        char a2[]={'s','f','e','e'};
        string array[] ={"car","sfadsf","ohklkk","iuruio"};
    
        cout<<"before sort:";
    
        for(int m=0;m<4;m++)
        {
            cout<<a1[m]<<" ";
        }
        insertSort(a1,4);
        cout<<endl<<"after sort:";
        for(int m=0;m<4;m++)
        {
            cout<<a1[m]<<" ";
        }
        cout<<endl<<endl;
        //////////////////////////////////////////
        cout<<"before sort:";
        for(int m=0;m<4;m++)
        {
            cout<<a2[m]<<" ";
        }
        insertSort(a2,4);
        cout<<endl<<"after sort:";
        for(int m=0;m<4;m++)
        {
            cout<<a2[m]<<" ";
        }
        cout<<endl<<endl;
        /////////////////////////////////////////
        cout<<"before sort:";
    
        for(int m=0;m<4;m++)
        {
            cout<<array[m]<<" ";
        }
        insertSort(array,4);
        cout<<endl<<"after sort:";
        for(int m=0;m<4;m++)
        {
            cout<<array[m]<<" ";
        }
        cout<<endl<<endl;
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by saravanansaravanan on Jan 24, 2010 at 10:19 p.m. EST
  • sorting strings
    void selectionSort(string list[], int arraySize)
    {
        //shin's selection sort
        int i;
        int j;
        int min;
        string temp;
    
        for (i = 0; i < arraySize-1; i++)
        {
            min = i;//smallest is always 0 in int
            for (j = i+1; j < arraySize; j++) //find the smallest
            {
                if (list[j] < list[min])
                min = j; //remember the position of the smalles
            }
    
            temp = list[i]; //put the curr item to a temp
            list[i] = list[min];//swapp smallest to the curr
            list[min] = temp; //swap the curr to the pos where smallest was found
        }
    }
    
    //shin's insertion sort
    void insertSort(string list[], int arraySize)
    {
        int i;
        int j;
        string temp;
    
        for (i = 1; i < arraySize; i++)
        //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
        {
            temp = list[i];//the item taken out for insertion, always from the unsorted list
            for (j = i - 1; j >= 0 && list[j] > temp; j--)
            {
                //for the items that are larger then the item taken out to be insert,
                // to make way for item to be inserted
                list[j + 1] = list[j];//move it out into the rhs item
            }
    
            list[j + 1] = temp;//insert the item
        }
    }
    
    // bubble sort
    void bubbleSort(string list[], int arraySize)
    {
        bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
            // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] > list[i + 1])
                {
                    // Swap list[i] with list[i + 1]
                    string temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }
    
    int main()
    {
        int sortselect;
        string list[] = {"Mary","Anne","Ecuador","Cadbury","Betty","Griffindor","Danny","Falter","Jockey","Iskandar"};
        int arraySize = 10;
    
        while(1)
        {
            cout << "Select sort function : " << endl;
            cout << "1) Bubble Sort" << endl << "2) Insertion Sort" << endl << "3) Selection Sort" << endl;
            cin >> sortselect;
    
            switch(sortselect)
            {
                case 1: bubbleSort(list, arraySize);
                        for(int i=0; i<10; i++)
                            cout << list[i] << " ";
                        cout << endl << endl;
                    break;
    
                case 2: insertSort(list, arraySize);
                        for(int i=0; i<10; i++)
                            cout << list[i] << " ";
                        cout << endl << endl;
                        break;
    
                case 3: selectionSort(list, arraySize);
                        for(int i=0; i<10; i++)
                            cout << list[i] << " ";
                        cout << endl << endl;
                    break;
            }
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by Rajendran on Jan 24, 2010 at 10:18 p.m. EST
  • selectionsort_string
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    void selectionSort(T list[], int arraySize)
    {
    //shin's selection sort
        int i;
        int j;
        int min;
        T temp;
    
        for (i = 0; i < arraySize-1; i++)
        {
            min = i;//smallest is always 0 in int
            for (j = i+1; j < arraySize; j++) //find the smallest
            {
                if (list[j] < list[min])
                    min = j; //remember the position of the smalles
            }
            temp = list[i]; //put the curr item to a temp
            list[i] = list[min];//swapp smallest to the curr
            list[min] = temp; //swap the curr to the pos where smallest was found
        }
    }
    
    int main()
    {
        //int array[] = {23, 2, 56, 89, 2, 42, 9, 11, 100, 35, 2, 17};
        //char array[] = {'a','k', 'o', 'y', 'f'};
        string array[] = {"ding", "fin", "ain", "and", "sun"};
    
        cout << "Before sorting..." << endl;
    
        for (int j=0; j<5; j++)
        {
            cout << array[j];
    
            if (j<4)
                cout << ", ";
        }
    
        cout << ".";
    
        cout << endl << endl;
    
        selectionSort(array, 5);
    
        cout << "After sorting..." << endl;
    
        for (int j=0; j<5; j++)
        {
            cout << array[j];
    
            if (j<4)
                cout << ", ";
        }
    
        cout << ".";
        cout << endl;
    
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by HuiHui on Jan 24, 2010 at 10:16 p.m. EST
  • insertionsort_char
    #include <iostream>
    
    using namespace std;
    
    //shin's insertion sort
    template <typename T>
    void insertSort(T a[], int size)
    {
        int i;
        int j;
        T temp;
    
        for (i = 1; i < size; i++)
    //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
        {
            temp = a[i];//the item taken out for insertion, always from the unsorted list
            for (j = i - 1; j >= 0 && a[j] > temp; j--)
            {
    //for the items that are larger then the item taken out to be insert,
    // to make way for item to be inserted
                a[j + 1] = a[j];//move it out into the rhs item
            }
            a[j + 1] = temp;//insert the item
        }
    }
    
    
    int main()
    {
        //int array[] = {23, 2, 56, 89, 2, 42, 9, 11, 100, 35, 2, 17};
        char array[] = {'a','k', 'o', 'y', 'f'};
    
        cout << "Before sorting..." << endl;
    
        for (int j=0; j<5; j++)
        {
            cout << array[j];
    
            if (j<4)
                cout << ", ";
        }
    
        cout << ".";
    
        cout << endl << endl;
    
        insertSort(array, 5);
    
        cout << "After sorting..." << endl;
    
        for (int j=0; j<5; j++)
        {
            cout << array[j];
    
            if (j<4)
                cout << ", ";
        }
    
        cout << ".";
        cout << endl;
    
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by HuiHui on Jan 24, 2010 at 10:15 p.m. EST
  • insertion sort string
    #include <iostream>
    
    using namespace std;
    
    //shin's insertion sort
    template <typename T>
    void insertSort(T a[], int size)
    {
        int i;
        int j;
        T temp;
    
        for (i = 1; i < size; i++)
    //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
        {
            temp = a[i];//the item taken out for insertion, always from the unsorted list
            for (j = i - 1; j >= 0 && a[j] > temp; j--)
            {
    //for the items that are larger then the item taken out to be insert,
    // to make way for item to be inserted
                a[j + 1] = a[j];//move it out into the rhs item
            }
            a[j + 1] = temp;//insert the item
        }
        //for(int m=0;m<5;m++)
        //cout<<a[m];
    }
    
    int main()
    {
        //int array[] ={9,4,2,8,1};
        string array[] ={"car","sfadsf","ohklkk","iuruio"};
    
        insertSort(array,4);
        for(int m=0;m<4;m++)
        {
            cout<<array[m]<<" ";
        }
    
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by saravanansaravanan on Jan 24, 2010 at 10:10 p.m. EST
  • selectionSort
    #include <iostream>
    using namespace std;
    
    
    
    void selectionSort(int list[], int arraySize)
    {
    //shin's selection sort
        int i;
        int j;
        int min;
        int temp;
    
        for (i = 0; i < arraySize-1; i++)
        {
            min = i;
            for (j = i+1; j < arraySize; j++) 
            {
                if (list[j] < list[min])
                    min = j; 
            }
            temp = list[i]; 
            list[i] = list[min];
            list[min] = temp; 
        }
    }
    
    int main()
    {
        int array1[]={23,2,56,89,2,42,9,11,100,35,2,17};
        //bubbleSort(array1,12);
        selectionSort(array1,12);
        for (int q=0;q<=12;q++)
        {
            cout<<q<<" ";
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by Fatal_eX on Jan 24, 2010 at 10:07 p.m. EST
  • character sort
    void selectionSort(char list[], int arraySize)
    {
        //shin's selection sort
        int i;
        int j;
        char min;
        char temp;
    
        for (i = 0; i < arraySize-1; i++)
        {
            min = i;//smallest is always 0 in int
            for (j = i+1; j < arraySize; j++) //find the smallest
            {
                if (list[j] < list[min])
                min = j; //remember the position of the smalles
            }
    
            temp = list[i]; //put the curr item to a temp
            list[i] = list[min];//swapp smallest to the curr
            list[min] = temp; //swap the curr to the pos where smallest was found
        }
    }
    
    //shin's insertion sort
    void insertSort(char list[], int arraySize)
    {
        int i;
        int j;
        char temp;
    
        for (i = 1; i < arraySize; i++)
        //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
        {
            temp = list[i];//the item taken out for insertion, always from the unsorted list
            for (j = i - 1; j >= 0 && list[j] > temp; j--)
            {
                //for the items that are larger then the item taken out to be insert,
                // to make way for item to be inserted
                list[j + 1] = list[j];//move it out into the rhs item
            }
    
            list[j + 1] = temp;//insert the item
        }
    }
    
    /* bubble sort */
    void bubbleSort(char list[], int arraySize)
    {
        bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
            // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] > list[i + 1])
                {
                    // Swap list[i] with list[i + 1]
                    char temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }
    
    int main()
    {
        int sortselect;
        char list[] = {'H','A','E','C','B','G','D','F','J','I'};
        int arraySize = 10;
    
        while(1)
        {
            cout << "Select sort function : " << endl;
            cout << "1) Bubble Sort" << endl << "2) Insertion Sort" << endl << "3) Selection Sort" << endl;
            cin >> sortselect;
    
            switch(sortselect)
            {
                case 1: bubbleSort(list, arraySize);
                        for(int i=0; i<10; i++)
                            cout << list[i];
                        cout << endl << endl;
                    break;
    
                case 2: insertSort(list, arraySize);
                        for(int i=0; i<10; i++)
                            cout << list[i];
                        cout << endl << endl;
                        break;
    
                case 3: selectionSort(list, arraySize);
                        for(int i=0; i<10; i++)
                            cout << list[i];
                        cout << endl << endl;
                    break;
            }
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by Rajendran on Jan 24, 2010 at 10:04 p.m. EST
  • decending sort
    void selectionSort(int list[], int arraySize)
    {
        //shin's selection sort
        int i;
        int j;
        int min;
        int temp;
    
        for (i = 0; i < arraySize-1; i++)
        {
            min = i;//smallest is always 0 in int
            for (j = i+1; j < arraySize; j++) //find the smallest
            {
                if (list[j] > list[min])
                min = j; //remember the position of the smalles
            }
    
            temp = list[i]; //put the curr item to a temp
            list[i] = list[min];//swapp smallest to the curr
            list[min] = temp; //swap the curr to the pos where smallest was found
        }
    }
    
    //shin's insertion sort
    void insertSort(int list[], int arraySize)
    {
        int i;
        int j;
        int temp;
    
        for (i = 1; i < arraySize; i++)
        //this case always choose the 2nd item from the array for a start, then the 3rd item and so on
        {
            temp = list[i];//the item taken out for insertion, always from the unsorted list
            for (j = i - 1; j >= 0 && list[j] < temp; j--)
            {
                //for the items that are larger then the item taken out to be insert,
                // to make way for item to be inserted
                list[j + 1] = list[j];//move it out into the rhs item
            }
    
            list[j + 1] = temp;//insert the item
        }
    }
    
    /* bubble sort */
    void bubbleSort(int list[], int arraySize)
    {
        bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
            // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] < list[i + 1])
                {
                    // Swap list[i] with list[i + 1]
                    int temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }
    
    int main()
    {
        int sortselect;
        int list[] = {1,9,3,5,2,8,7,4,6,0};
        int arraySize = 10;
    
        cout << "Select sort function : " << endl;
        cout << "1) Bubble Sort" << endl << "2) Insertion Sort" << endl << "3) Selection Sort" << endl;
        cin >> sortselect;
    
        switch(sortselect)
        {
            case 1: bubbleSort(list, arraySize);
                    for(int i=0; i<10; i++)
                        cout << list[i];
                break;
    
            case 2: insertSort(list, arraySize);
                    for(int i=0; i<10; i++)
                        cout << list[i];
                break;
    
            case 3: selectionSort(list, arraySize);
                    for(int i=0; i<10; i++)
                        cout << list[i];
                break;
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by Rajendran on Jan 24, 2010 at 9:59 p.m. EST
  • bubblesort
    #include <iostream>
    #include <queue>
    using namespace std;
    
    void bubbleSort(int list[], int arraySize)
    {
        bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
    // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] > list[i + 1])
                {
    // Swap list[i] with list[i + 1]
                    int temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }
    
    int main()
    {
        int array1[]={23,2,56,89,2,42,9,11,100,35,2,17};
        bubbleSort(array1,12);
        for(int q=0;q<=12;q++)
        {
            cout<<q<<" ";
        }
    }
    

    copy | embed

    0 comments - tagged in  posted by Fatal_eX on Jan 24, 2010 at 9:58 p.m. EST
  • bubble sort desending
    #include <iostream>
    
    using namespace std;
    
    /* bubble sort */
    void bubbleSort(int list[], int arraySize)
    {
    bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
                        // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] < list[i + 1])
                {
                    // Swap list[i] with list[i + 1]
                    int temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
        for(int j=0;j<5;j++)
        cout<<list[j];
    }
    
    int main()
    {
        int array[]={1,7,4,3,8};
        bubbleSort(array, 5);
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by saravanansaravanan on Jan 24, 2010 at 9:56 p.m. EST
  • bubblesort_int
    #include <iostream>
    
    using namespace std;
    
    /* bubble sort */
    void bubbleSort(int list[], int arraySize)
    {
        bool needNextPass = true;
    
        for (int k = 1; k < arraySize && needNextPass; k++)
        {
    // Array may be sorted and next pass not needed
            needNextPass = false;
            for (int i = 0; i < arraySize - k; i++)
            {
                if (list[i] > list[i + 1])  //ascending
                //if (list[i] < list[i + 1])  //descending
                {
    // Swap list[i] with list[i + 1]
                    int temp = list[i];
                    list[i] = list[i + 1];
                    list[i + 1] = temp;
    
                    needNextPass = true; // Next pass still needed
                }
            }
        }
    }
    
    int main()
    {
        int array[] = {23, 2, 56, 89, 2, 42, 9, 11, 100, 35, 2, 17};
    
        cout << "Before sorting..." << endl;
    
        for(int j=0; j<12; j++)
        {
            cout << array[j];
    
            if(j<11)
                cout << ", ";
        }
    
        cout << ".";
    
        cout << endl << endl;
    
        bubbleSort(array, 12);
    
        cout << "After sorting..." << endl;
    
        for(int j=0; j<12; j++)
        {
            cout << array[j];
    
            if(j<11)
                cout << ", ";
        }
    
        cout << ".";
        cout << endl;
    
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by HuiHui on Jan 24, 2010 at 9:54 p.m. EST
  • sort by name
    #include <iostream>
    #include <list>
    using namespace std;
    
    class Student
    {
        public:
        string Name;
        double gpa;
    };
    
    bool sortByName(Student &A, Student &B)
    {
        return (A.Name < B.Name);
    }
    
    
    int main()
    {
        list<Student> SList;
    
        Student S;
        for (int i=0; i<5; i++)
        {
            cout << "Enter the Name, and YOUR GPAs: ";
            cin >> S.Name >> S.gpa ;
            cout << endl;
            SList.push_back(S);
        };
        cout << "List of student records created." << endl;
    
        SList.sort(sortByName);
        list<Student>::iterator SIt;
        SIt = SList.begin();
        while (SIt !=SList.end())
        {
            cout << (*SIt).Name << " "  << (*SIt).gpa << endl;
            SIt++;
        }
    
        return 0;
    }
    

    copy | embed

    0 comments - tagged in  posted by HD on Jan 11, 2010 at 8:58 p.m. EST
  • Linked List GPA sort
    class Student
    {
        public:
        string Name;
        double GPA;
    };
    
    bool sortByGPA(Student &A, Student &B)
    {
        return (A.GPA < B.GPA);
    }
    
    int main()
    {
        int found;
        string findname;
        list<Student> ListA;
    
        Student A;
        for (int i=0; i<5; i++)
        {
            cout << "Enter the Name and GPA of student: ";
            cin >> A.Name >> A.GPA;
            cout << endl;
            ListA.push_back(A);
        };
    
        ListA.sort(sortByGPA);
        list<Student>::iterator it;
        it = ListA.begin();
    
        while (it !=ListA.end())
        {
            cout << (*it).Name << " " <<(*it).GPA << endl;
            it++;
        }
    
        cout << endl << "Find student: ";
        cin >> findname;
    
        for(it = ListA.begin(); it != ListA.end(); it++)
        {
            if(findname == (*it).Name)
            {
                cout << "Found!";
                found = 1;
            }
        }
    
        if(found == 0)
            cout << "Not Found";
    
    }
    

    copy | embed

    0 comments - tagged in  posted by Rajendran on Jan 11, 2010 at 8:43 p.m. EST
  • sort
    #include <iostream>
    #include <list>
    
    
    using namespace std;
    
    
    int main()
    {
        list<int> list1;
    
        list<int> :: iterator it;
    
        list1.push_back(10);
        list1.push_back(30);
        list1.push_back(20);
        list1.push_back(40);
        list1.push_back(50);
    
    
        for(it=list1.begin(); it!=list1.end(); it++)
    
        cout<<*it<<" ";
        cout<<endl;
    
        list1.sort();
            for(it=list1.begin(); it!=list1.end(); it++)
    
        cout<<*it<<" ";
    
    }
    

    copy | embed

    0 comments - tagged in  posted by weijikuan on Jan 11, 2010 at 8:25 p.m. EST
Sign up to create your own snipts, or login.