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