简体   繁体   中英

Issue with c# quick sort algorithm

I have created two functions that sorts a list using bubble sort, but I would like to change the sort style to quick sort.

I found this quick sort algorithm

http://snipd.net/quicksort-in-c

These are my two functions:

    protected void sort_by_section_name()
    {
        int num1, num2;
        for (var i = section_names.Count - 1; i > 0; i -= 1)
        {
            for (var j = 0; j < i; j += 1)
            {
                num1 = get_number_from_section(section_names[j]);
                num2 = get_number_from_section(section_names[j + 1]);
                if (num1 > num2)
                {
                    swap_list_strings(section_names, j, j + 1);
                    swap_object_items(item_group_list, j, j + 1);
                }
            }
        }
    }

    protected void sort_items()
    {
        int num1, num2;
        List<SPListItem> temp;
        for (var k = 0; k < item_group_list.Count; k += 1)
        {
            temp = (List<SPListItem>)item_group_list[k];
            for (var i = temp.Count - 1; i > 0; i -= 1)
            {
                for (var j = 0; j < i; j += 1)
                {
                    num1 = Convert.ToInt32((temp[j])[ORDER_BY_COLUMN]);
                    num2 = Convert.ToInt32((temp[j + 1])[ORDER_BY_COLUMN]);
                    if (num1 > num2)
                    {
                        swap_list_items(temp, j, j + 1);
                    }
                }
            }
        }
    }

For sort_items, its an array of arrays, so the bubble sort stuff is in a for loop.

I don't understand how to change these two functions into using the quicksort.

Can someone please help me?

You don't need to write it yourself in .NET - you can use:

  1. Array.Sort for a basic array of items
  2. LINQ - OrderBy for example with a List<string> (make sure you have using System.Linq at the top of the class)
  3. If you're feeling adventurous, look into IComparable
  4. Use myItems.Sort() which sorts them in place.

For what you want, the easiest way to get started is using #2, here's an example:

List<SPListItem> myItems = GetSomeItems();
myItems = myItems.OrderBy(i => i["MyField"]).ToList();

foreach (var item in sortedItems)
    Console.WriteLine(item);

Without knowing the fields you're after, or much about the Sharepoint object that's a bit of a guess, there's are about 5 different ways of doing it in .NET with comparable interfaces (some more info here ). As you can't change the SPListItem class then Sort or LINQ maybe easiest.

So you have a List<SPListItem> and you want them sorted, using an efficient sorting algorithm (aka not bubblesort) based on the numeric value of some field. This is easy, and doesn't involve you re-implementing quicksort.

List<SPListItem> list = ...;

var sortedData = list.OrderBy(item => Convert.ToInt32(item["fieldName"]));

It's also worth noting that when possible it's usually better to sort your data on the database, rather than on the webserver. You should be able to add an Order By clause to the CAML query generating those SPListItem s and let it do the sort.

It appears that you're sorting two different data structures that are "parallel" (the item at the same index of both structures "belong" together). This is generally undesirable. While there are ways to perform a sort on both structures, what you really should be doing is making a single structure such that each item holds onto everything that logically represents that one item. In many cases this means creating a new class that has properties for each piece of data. You can then populate a collection of this new composite class and sort that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM