JAVA Implementation of Common sorting algorithms.

Source: Internet
Author: User

My wife decided to take a postgraduate student from Tongji University, So I reviewed the data structure and prepared to answer questions for her. After learning, I think we should sort out the Sorting Algorithm system, so I used Java to implement the Sorting Algorithm in data structure and Algoritm Analysis in C. I hope someone can help me correct the error.

The entire structure I use is the strategy mode, which is a clear choice. The sort class assumes the environment role, and sortstrategy assumes the abstract policy role. There are six specific policy roles: insertsort, bubblesort, choosesort, shellsort, mergesort, and quicksort. Insert sorting, Bubble sorting, selection sorting, Hill sorting, Merge Sorting, and quick sorting. There are also heap sorting, two-way Bubble sorting, and so on. I haven't written them yet, and I will post them after writing them.

Because of the large amount of code, I can paste the code several times. This time, I only paste the code of sort and sortstrategy.

Sortstratey interface:

Package utils. Sort;

/**

* Sorting algorithm interface

*/

Interface sortstrategy

{

/**

* Various algorithms are used to sort the arrays that implement the comparable interface in ascending order.

*/

Public void sort (comparable [] OBJ );

}

Sort class:

Package utils. Sort;

/**

* Sorting class. The sort () class can be used to sort the arrays that implement the comparable interface in ascending order.

*/

Public class sort

{

Private sortstrategy strategy;

 

/**

* Constructor: The type determines the algorithm used for sorting. The word in the sorting method must be uppercase or lowercase. For quick sorting, it should be

Uicksort

* @ Param type: Type of the Sorting Algorithm

*/

Public sort (string type)

{

Try

{

Type = "utils. Sort." + type. Trim ();

Class C = Class. forname (type );

Strategy = (sortstrategy) C. newinstance ();

}

Catch (exception E)

{

E. printstacktrace ();

}

}

/**

* The sorting method requires that the array to be sorted must implement the comparable interface.

*/

Public void sort (comparable [] OBJ)

{

Strategy. Sort (OBJ );

}

}

The links of the other classes are:

Insert sort: http://blog.csdn.net/Linyco/archive/2005/10/10/498735.aspx

Bubble sort: http://blog.csdn.net/Linyco/archive/2005/10/10/498736.aspx

Select sort: http://blog.csdn.net/Linyco/archive/2005/10/10/498742.aspx

Merge sort: http://blog.csdn.net/Linyco/archive/2005/10/10/498746.aspx

Hill sorting: http://blog.csdn.net/Linyco/archive/2005/10/10/498764.aspx

Quick Sort: http://blog.csdn.net/Linyco/archive/2005/10/10/498759.aspx

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.