Latest 100 public
snipts » sort
showing 1-20 of 49 snipts for sort
-
∞ binary search
#include <iostream> using namespace std; template <typename K> int bsearch(K temp[], K searchforthis, int low, int high) //recursive search { int mid; mid=low+((high-low)/2); //find middle if (temp[mid] > searchforthis) //if middle value is more than what you are searching return bsearch(temp, searchforthis, low, mid-1); //then return back to function with middle value -1 as new high else if (temp[mid] < searchforthis) //or if middle value is less than what you are searching return bsearch(temp, searchforthis, mid+1, high); //then return back to function with middle value +1 as new low else return mid; } int main() { char anarray[]={'Z','F','E','R','G','P','D','A'}; char term; term='D'; sort(anarray,anarray+(sizeof(anarray))); cout<<"Sorted: "; for (int i=0;i<8;i++) cout<<anarray[i]; cout<<"\nTerm is found at position "<<bsearch(anarray,term,0,sizeof(anarray))<<endl; return 0; }
-
∞ sort using own function
#include <iostream> #include <functional> using namespace std; bool descfunction (int a,int b); int main() { int array[5]={5,2,6,8,1}; sort(array, array+5, descfunction); for (int i=0;i<5;i++) cout<<array[i]<<endl; return 0; } bool descfunction (int a,int b) { return (a>b); }
-
∞ sorting q1
#include <iostream> #include <queue> using namespace std; void bubbleSort(int list[], int arraySize); void insertSort(int a[], int size); void selectionSort(int list[], int arraySize); int main() { int array[5]={5,2,6,8,1}; // bubbleSort(array, 5); // insertSort(array, 5); // selectionSort(array, 5); // sort(array, array+5); // reverse(array, array+5); for (int i=0;i<5;i++) cout<<array[i]<<endl; return 0; } /* bubble sort (descending)*/ 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 } } } } //shin's insertion sort (descending) 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 } } void selectionSort(int list[], int arraySize) { //shin's selection sort (descending) 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 } }
-
∞ exercise on queue 4
#include <iostream> #include <queue> using namespace std; int main() { deque<int>myqueue; deque<int>::iterator it; vector<int>myvector; vector<int>::iterator it2; for (int i=1;i<11;i++) //using vector to store temp myvector.push_back(i); random_shuffle(myvector.begin(),myvector.end()); //stl for "random" priority for (it2=myvector.begin() ; it2<myvector.end() ; it2++ ) { myqueue.push_back(*it2); } sort(myqueue.begin(),myqueue.end()); //sort according to priority for (it=myqueue.begin() ; it<myqueue.end() ; it++ ) //print { cout<<*it<<endl; } return 0; }
-
∞ list sorting 2
#include <iostream> #include <list> using namespace std; int main() { list<string> list1; list1.push_back("Alex"); //Input data list1.push_back("Brendon"); list1.push_back("Caren"); list1.push_back("Dwight"); list1.push_back("Eugene"); list1.push_back("Francis"); list<string>::iterator l1; //Print out list for (l1=list1.begin();l1!=list1.end();l1++) cout<<*l1<<endl; string findthis; //Search for name cout<<"\nEnter name to search: "; cin>>findthis; l1=find(list1.begin(),list1.end(),findthis); if (l1==list1.end()) //find input name using stl find function cout<<findthis<<" not found!"<<endl; else cout<<findthis<<" found!"<<endl; string temp, temp2; for (l1=list1.begin();l1!=list1.end();l1++) { cout<<"\nEnter gpa for "<<*l1<<": "; cin>>temp; temp2=temp+" "+*l1; list1.insert(l1,temp2); //store new element(gpa space name) l1=list1.erase(l1); //remove original name (without gpa) l1--; } list1.sort(); //sort using stl list1.reverse(); //rank descending cout<<"\nSorted by gpa, decending: "<<endl; //Print out list for (l1=list1.begin();l1!=list1.end();l1++) cout<<*l1<<endl; return 0; }
-
∞ list sorting
#include <iostream> #include <list> #include <algorithm> using namespace std; int main() { list<string> list1; list1.push_back("Alex"); //Input data list1.push_back("Brendon"); list1.push_back("Caren"); list1.push_back("Dwight"); list1.push_back("Eugene"); list1.push_back("Francis"); list<string>::iterator l1; //Print out list for (l1=list1.begin();l1!=list1.end();l1++) cout<<*l1<<endl; string findthis; //Search for name cout<<"\nEnter name to search: "; cin>>findthis; list<string>::iterator fnd; fnd = find(list1.begin(),list1.end(),findthis); if (fnd==list1.end()) //find input name using stl find function cout<<findthis<<" not found!"<<endl; else cout<<findthis<<" found!"<<endl; string temp, temp2; list<string>::iterator l3; for (l3=list1.begin();l3!=list1.end();l3++) { cout<<"\nEnter gpa for "<<*l3<<": "; cin>>temp; temp2=temp+" "+*l3; list1.insert(l3,temp2); //store new element(gpa space name) list1.remove(*l3); //remove original name (without gpa) } list1.sort(); //sort using stl list1.reverse(); //rank descending list<string>::iterator l4; //Print out list cout<<"\nSorted by gpa, decending: "<<endl; for (l4=list1.begin();l4!=list1.end();l4++) cout<<*l4<<endl; // return 0; }
-
∞ A very odd kind of sort!
#include <conio.h> #include <iostream> using namespace std; void bubble(int array[],int length) { int i,j,temp; for(i=length-1;i>0;i--) for(j=0;j<i;j++) if(array[j]>array[j+1]) { temp = array[j]; array[j]=array[j+1]; array[j+1]=temp; } } int main() { int negativeeven[10000],positiveeven[10000],negativeodd[10000],positiveodd[10000]; int num=0,ne=0,pe=0,no=0,po=0; cout<<"\n"<<"How many numbers do you like to enter?"; cin>>num; for(int i=0;i<num;i++) { cout<<"\nPlease enter number["<<i+1<<"] :"; int number; cin>>number; if(number<0) { if((number%2)==0) { negativeeven[ne]=number; ne++; } else { negativeodd[no]=number; no++; } } if(number>0) { if((number%2)==0) { positiveeven[pe]=number; pe++; } else { positiveodd[po]=number; po++; } } } bubble(positiveodd,po); bubble(positiveeven,pe); bubble(negativeodd,no); bubble(negativeeven,ne); cout<<"\n\r\a"; for(int i=0;i<ne;i++) cout<<"\t"<<negativeeven[i]; for(int i=0;i<po;i++) cout<<"\t"<<positiveodd[i]; for(int i=0;i<no;i++) cout<<"\t"<<negativeodd[i]; for(int i=0;i<pe;i++) cout<<"\t"<<positiveeven[i]; cout<<"\nPress any key to exit."; getch(); return 0; }
-
∞ Bucket sort Algorithm
public virtual int[] BucketSorting(int[] integers) { //Verify input if (integers == null || integers.Length == 0) { return null; } //Find the maximum and minimum values in the array int maxValue = integers[0]; //start with first element int minValue = integers[0]; //Note: start from index 1 // cause we have already initialized max and min to 0 index ;) for (int i = 1; i < integers.Length; i++) { if (integers[i] > maxValue) maxValue = integers[i]; if (integers[i] < minValue) minValue = integers[i]; } //Create a temporary "bucket" to store the values in order //each value will be stored in its corresponding index //scooting everything over to the left as much as possible (minValue) //e.g. 34 => index at 34 - minValue //List uses more memory hence use linked list //List<int>[] bucket = new List<int>[maxValue - minValue + 1]; LinkedList<int>[] bucket = new LinkedList<int>[maxValue - minValue + 1]; //Initialize the bucket // but doing so , uses lot of memery //even though that bucket may had no element ( hence no requirement to initialize) /* for (int i = 0; i < bucket.Length; i++) { bucket[i] = new List<int>(); } */ //Move items to bucket for (int i = 0; i < integers.Length; i++) { if (bucket[integers[i] - minValue] == null) { //bucket[integers[i] - minValue] = new List<int>(); bucket[integers[i] - minValue] = new LinkedList<int>(); } // bucket[integers[i] - minValue].Add(integers[i]); bucket[integers[i] - minValue].AddLast(integers[i]); } //Move items in the bucket back to the original array in order int k = 0; //index for original array /* for (int i = 0; i < bucket.Length; i++) { if (bucket[i] != null && bucket[i].Count > 0) { for (int j = 0; j < bucket[i].Count; j++) { integers[k] = bucket[i][j]; k++; } } } * */ for (int i = 0; i < bucket.Length; i++) { if (bucket[i] != null) { LinkedListNode<int> node = bucket[i].First; //start add head of linked list while (node != null) { integers[k] = node.Value; //get value of current linked node node = node.Next; //move to next linked node k++; } } } return integers; }
-
∞ Show running processes ordered by memory consumption
ps -eo pmem,pcpu,rss,vsize,args | sort -k 1 -r
-
∞ sort a *nix directory listing by size
ls -al | sort -nk5
-
∞ Find the most recently changed files recursively
find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort -
∞ number of file open by command
lsof | awk '{printf("%s\n",$1)}' | sort | uniq -c | sort -n -
∞ 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 } }
-
∞ 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++; } }
-
∞ 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; }
-
∞ 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; }
-
∞ 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; }
-
∞ 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; }
-
∞ 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; }
-
∞ 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; } } }


