how to sort ?

This is a discussion on how to sort ? within the C++ Programming forums, part of the General Programming Boards category; Assume there are two arrays arr1[] arr2[] 1 0 2 5 2 0 2 10 4 9 4 9 5 ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    how to sort ?

    Assume there are two arrays

    arr1[] arr2[]
    1 0
    2 5
    2 0
    2 10
    4 9
    4 9
    5 0

    i need to sort arr1[] in such a way the resultant array is

    5 4(9) 4(9) 2(10) 2(5) 2(0) 1 // preference is given to number in arr1 if equal the arr2 is consulted

    is there any way to do this using STL in C++ ??

    plz help !
    thank u !

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Given that the C++ STL has something called "sort" it's possible. However, you would have to tie the two numbers together into one object. Otherwise you'll have to roll your own sorting function (not that that's exceptionally difficult).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    re :

    This doesnot work !

    Code:
    #include <cstdio>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector< pair<int,int> >arr(101);
    int main()
    {
    	int i,a,b,c,n;
    	
    	cin>>n;
    	arr.clear();
    	for(i=0;i<n;i++)
    	{
    		cin>>a>>b;
    		arr[i].first=a;
    		arr[i].second=b;
    	}
    	sort(arr.begin(),arr.end());
    	
    	for(i=0;i<n;i++)
    		cout<<arr[i].first<<" "<<arr[i].second<<endl;
    return 0;
    }

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    You're sorting the entire array, but only accepting n inputs.

    Also, if the < operator for std::​pair doesn't do what you want (or, if it isn't even defined, which I suspect may be the case), then you'll need to supply a predicate to std::sort. (A predicate is a class with bool operator () (item_a, item_b) defined)
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The operator < for pairs is in the <utility> header (I had to look that up). And if n does not equal 101 then it's not going to work anyway -- you should use arr[n] instead of arr.end() in your sort.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    [
    Code:
    #include <cstdio>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <utility>
    using namespace std;
    
    vector< pair<int,int> >arr;
    int main()
    {
    	int i,a,b,c,n;
    	
    	cin>>n;
    	arr.clear();
    	for(i=0;i<n;i++)
    	{
    		cin>>a>>b;
    		arr.push_back(make_pair(-a,-b));
    	}
    	sort(arr.begin(),arr.end());
    	
    	for(i=0;i<n;i++)
    		cout<<-arr[i].first<<" "<<-arr[i].second<<endl;
    	return 0;
    }
    Pair has a built-in sort, to reverse the order just negate it (and back again for output).
    The cost of software maintenance increases with the square of the programmer's creativity.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    Thanks

    Might seem a bit foolish to thank after a long time..but still !!

    Thanks 0rion

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM
  2. threaded merge sort
    By AusTex in forum Linux Programming
    Replies: 4
    Last Post: 05-04-2005, 04:03 AM
  3. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 04:21 PM
  4. radix sort and radix exchange sort.
    By whatman in forum C Programming
    Replies: 1
    Last Post: 07-31-2003, 12:24 PM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21