Project Application of C # bubbling, fast, select, and insert sorting algorithms

Source: Internet
Author: User

In a previous article, we have simply implemented four sort algorithms for one-dimensional arrays, but in actual projects, the way we sort may be (almost certainly) more than just sorting by numbers, we often sort by the right sort of order, such as flight information may be sorted by time, Commodity information may be sorted by price and so on. The following improved code in the "C # Bubble, fast, select, and insert sort algorithm" in the previous article, which enables you to sort the different objects (car in the instance) by different sort types (the price and name in the instance). Well, the Code is cheap. Look at the code:

using System;


using System.Collections;


using System.Collections.Generic;





namespace Sorter


{


//Declaration delegates, you can select sort by sorting (not limited to sort by integer)


public delegate bool Compareoperation (object Carprev,object carnext);


///<summary>


///cars are used to build an array of cars sorted by price or car name


///</summary>


public class Car


    {


private string name;


public string Name


        {


get {return name;}


set {name = value;}


        }


private decimal price;


Public decimal Price


        {


get {return price;}


set {price = value;}


        }


public Car (string name, decimal price)


        {


this.name = name;


this.price = Price;


        }


public override string ToString ()


        {


return string. Format ("Name:{0},price: {1:c}", name, price);


        }


///<summary>


///converts object to car class, according to the name of the vehicle


///</summary>


///<param name= "Carprev" ></param>


///<param name= "Carnext" ></param>


///<returns></returns>


public static bool Orderbycarname (object Objprev, Object Objnext)


        {


Car Carprev = (car) Objprev;


Car carnext = (car) objnext;


return (CarPrev.name.CompareTo (Carnext.name) < 0)? True:false;


        }





///<summary>


///converts object to car class, according to price comparison


///</summary>


///<param name= "Carprev" ></param>


///<param name= "Carnext" ></param>


///<returns></returns>


public static bool Orderbycarprice (object Objprev, Object Objnext)


        {


Car Carprev = (car) Objprev;


Car carnext = (car) objnext;


Return (Carprev.price < Carnext.price)? True:false;


        }


    }





///<summary>


///Bubble Sort


///</summary>


public class Bubblesorter


    {


static public void Sort (object[] Objarr, compareoperation sortop)


        {


BOOL flag = FALSE; Exchange Sign


for (int i = 1; i < objarr.length i++)//up to do objarr.length-1 trip sort


            {


flag = false;


for (int j = objarr.length-1 J >= i; j--)//scan the current unordered region from the bottom up


                {


if (Sortop (objarr[j), objarr[j-1))//Current unordered area: light below, "bubbling" to the top


                    {


object tmpobj = Objarr [j];


OBJARR[J] = objarr[j-1];


objarr[j-1] = tmpobj;


flag = true;


                    }


                }


if (!flag)//If no exchange occurs, terminate the algorithm


return;


            }


        }


    }





///<summary>


///Quick Sort


///</summary>


public class QuickSort


    {


public static void Sort (object[] Objarr, compareoperation sortop)


        {


Sort (Objarr, sortop, 0, objarr.length-1);


        }





private static void Sort (object[] Objarr, compareoperation sortop, int left, int right)


        {


if (left < right)


            {


Object middle = objarr[(left + right)/2];


int i = left-1;


Int J = right + 1;


while (true)


                {


while (Sortop (Objarr[++i], middle));





while (Sortop (middle, objarr[--j));





if (i >= j)


break;





Swap (Objarr, I, J);


                }


Sort (Objarr, Sortop, left, i-1);


Sort (Objarr, Sortop, J + 1, right);


            }


        }





private static void Swap (object[] Objarr, int i, int j)


        {


object tmpobj = Objarr[i];


Objarr[i] = objarr[j];


Objarr[j] = tmpobj;


        }


    }





///<summary>

The basic idea of
///Select sort (Selection sort) is that each trip selects the smallest key record from the record to be sorted, in the order that it is placed at the end of the sorted sub file until all the records have finished sorting.


///</summary>


public class Selectionsort


    {


static public void Sort (object[] Objarr, compareoperation sortop)


        {


int min;


for (int i = 0; i < objarr.length-1; i++)


            {


min = i;


for (int j = i + 1; j < Objarr.length J + +)


                {


if (Sortop (objarr[j), Objarr [min])


                    {


min = j;


                    }


                }


object tmpobj = Objarr[i];


Objarr[i] = objarr[min];


Objarr[min] = tmpobj;


            }


        }


    }





///<summary>

The
///insertion sort (insertion sort) algorithm description is a simple and intuitive sort algorithm. It works by building ordered sequences, for unsorted data,

The
///scans the sorted sequence from behind, finds the appropriate location and inserts it. Insert sort on implementation, usually using in-place sort (that is, a sort of extra space with O (1) only),


///Thus, in the process of scanning from the back forward, it is necessary to repeatedly move the sorted elements back and forth, providing inserting space for the newest elements.


///</summary>


public class Insertsort


    {


static public void Sort (object[] Objarr, compareoperation sortop)


        {


for (int i = 1; i < objarr.length i++)//i starting from 1


            {


Object t = Objarr[i]; Flag currently unsorted data


int j = i;


while ((J > 0) && (sortop (t,objarr[j-1)))


                {


Objarr[j] = objarr[j-1];


j--;


                }


objarr[j] = t; Inserts the current value in a sorted sequence


            }


        }


    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.