sorting

C Array sorting tips

a=[1,3,6,7,1,2] Which is the best sorting technique to sort the following array and if there are duplicates how to handle them. Also which is the best sorting technique of all.... void BubbleSort(int a[], int array_size) { int i, j, temp; for (i = 0; i < (array_size - 1); ++i) { for (j = 0; j < array_size - 1 - i; ++...

Sorting array of type of object respecting to one of the object values

i have a class called student, the class have two elements (Name, ID). in my main class, i have created about 10 students in an array i called it students, now i want to sort the the students array respecting the ID, or the name!! if i used this line of code array.sort(students); is there a way to use the sort method and choose whi...

anyone know how to NOT slice the Queryset with the Django Pagination object?

My django-sorting results are pathetic. Won't sort the data in the entire Queryset because djangos pagination model slices it. Any know a way around this? More details: http://code.google.com/p/django-pagination/issues/detail?id=37 ...

Array Sort Comparator method always does a default comparison

I have a class student - int age , int height and Name; I have n objects of the student class and I try to sort then first by age , if there is a tie then by height , if there is a tie randomize name . I have a class class StudentComparator implements Comparator{ public int compare(Object 1, Object2) { // Logic } } I ha...

How does a sorting network beat generic sorting algorithms?

In reference to fastest sort of fixed length 6 int array, I do not fully understand how this sorting network beats an algorithm like insertion sort. Form that question, here is a comparison of the number of CPU cycles taken to complete the sort : Linux 32 bits, gcc 4.4.1, Intel Core 2 Quad Q8300, -O2 Insertion Sort (Daniel S...

Linked list insertion sort

I'm not very advanced in the sorting part of programming yet, so I was looking for some help with my algorithm. void sortList() { Item_PTR tmpNxt = current->nextItem; Item_PTR tmpPTR = current; int a, tmp; while(tmpNxt != NULL) { a = tmpPTR->value; while(tmpNxt != tmpPTR && tmpNxt->value < a) ...

Query for results ordered by column of association table, most recent association only

given the following schema class User has_many :page_views #[id] end class Page has_many :page_views #[id] end class PageView belongs_to :user belongs_to :page #[id, created_at, updated_at] end How can i query for a given page, all users most recent page views in order by created_at date. One row per user, showing only...

Standard sorting networks for small values of n

I'm looking for a sorting network implementation of a 5-element sort, but since I couldn't find a good reference on SO, I'd like to ask for sorting networks for all small values of n, at least n=3 through n=6 but higher values would be great too. A good answer should at least list them as sequences of "swap" (sort on 2 elements) operatio...

Issue with qsort() - The sorting is not properly done (C)

Hello, I have a question regarding qsort. This is a little bit weird, but my qsort function does not give me the correct output. The strange thing is that some of my compare functions are identical to my past projects but they don't give me the correct input at all. I am not sure how to test it. For example: int comp_name_asc(const v...

Optimal median of medians selection - 3 element blocks vs 5 element blocks?

I'm working on a quicksort-variant implementation based on the Select algorithm for choosing a good pivot element. Conventional wisdom seems to be to divide the array into 5-element blocks, take the median of each, and then recursively apply the same blocking approach to the resulting medians to get a "median of medians". What's confusi...

How to quicksort over multiple columns

I'm looking to quicksort some objects in php. i'm sorting an array of OBJECTS $object->x; $object->y; $object->z; I want to first sort by x, then y, then z. This is my quicksort function Where it accepts an array of jobjects, and sorts by a particular sortkey (x, y, or z column) The function returns a sorted array of objects, that h...

Multi-Attribute Sorting and Filtering on an Array of Structs

I have an array of structs. Each struct has the following two attributes: win % # of wins I want to sort the array of structs by win %; however, for only those structs with at least 3 wins. Any suggestions? ...

External memory merge sort

Can anyone point me to a good reference on External Memory Mergesort? I've read the wiki page but am having trouble understanding it exactly. An animation might help but I can't seem to find one. Basically, I know that you have a certain number of blocks on disk, and you can fit a certain number of blocks in memory. Lets say you have...

Android sqlite: order by to sort international characters

Hi, I have a android database and it has a column called 'name'.'Name' column can have international characters. Now when I query this database, I want to sort the name column. The sort should consider international characters while sorting. I have read that there is a UNICODE collator for android but I am not able to use it in queries....

Insertion Sort on an array of strings in C#

If I have an array of strings, such as string[] names = {"John Doe", "Doe John", "Another Name", "Name Another"}; How do I sort this array, using insertion sort? Wikipedia has some examples: https://secure.wikimedia.org/wikibooks/en/wiki/Algorithm_implementation/Sorting/Insertion_sort#C.23 static void InsertSort(IComparable[] array)...

easiest way to make only one column sortable

I have a datagrid with sorting. I set enable sorting to true, but that makes every column sortable. Is there a simple way to make it so that only one column header can be clicked for sorting? I feel like there should be a simple and quick fix for this, but who knows. some code: <asp:GridView ID="ProductsGrid" runat="server" ...

How to bubble sort through a text file in Java

How do I do a bubble sort in java, The text file looks like this: aaa 2 bbb 3 ccc 1 What I need to do is to loop through it and display the highest score to the lowest. When I run the code below, The numbers, are already sorted. and it will display 3, 2, then 1. But the name isn't in sync with the corresponding score, it will onl...

Sorting an NSArray of NSDictionary

I have to sort an array of dictionaries but I have to order by an object in the dictionaries. ...

Big data sort and search

Hi, I have two files of data, 100 char lines each. File A: 10^8 lines, file B: 10^6 lines. And I need to find all the strings from file B that are not in file A. At first I was thinking feeding both files to mysql, but it looks like it won't ever finish creating an unique key on 10^8 records. I'm waiting for your suggestions on this. ...

How do you implement sorting and paging on distributed data?

Here's the problem I'm trying to solve: I need to be able to display a paged, sorted table of data that is stored across several database shards. Paging and sorting are well known problems that most of us can solve in any number of ways when the data comes from a single source. But if you're splitting your data across shards or using ...