ベースの並べ替え
3262 ワード
技術並べ替えの原理と考え方:数の下位から高位まで順を追って並べ替えて、
自分でチェーンを作って構造を実現します。
自分でチェーンを作って構造を実現します。
package ;
public class RadisSort {
public static void main(String[] args) {
//
int[] ary = new int[] { 135, 242, 192, 93, 345, 11, 24, 19 };
sort(ary);
}
//
static void sort(int[] ary){
Struts head = new Struts();
int max = 0;//
//
head.setData(ary[0]);
Struts p, q;
p = head;
for (int i = 1; i < ary.length; i++) {
if(max0){
max = max/10;
times++;
}
for (int i = 1; i <= times; i++) {
head = radis(head, i);
}
out(head);
}
/**
*
*
* @param radis
*/
static void out(Struts radis) {
while (radis != null) {
System.out.print(radis.getData() + " ");
radis = radis.getNext();
}
System.out.println();
}
/**
*
*
* @param head
* @param wei
*/
static Struts radis(Struts head, int wei) {
// 0-9
Struts[] HNB = new Struts[10];
// 0~9
Struts[] HNE = new Struts[10];
Struts headtemp = null;
;
//
int jishu1 = 1, jishu2, j = 0;
boolean bj = true;
while (--wei > 0) {
jishu1 *= 10;
}
jishu2 = jishu1 * 10;
Struts p;
p = head;
while (p != null) {
int index = (p.getData() % jishu2) / jishu1;
//
if (HNB[index] == null) {
// p, p;
HNB[index] = p;
HNE[index] = p;
} else {
// 。
HNE[index].setNext(p);
HNE[index] = p;
}
p = p.getNext();
}
for (int i = 0; i < HNB.length; i++) {
if (HNB[i] != null) {
//
if (bj) {
headtemp = HNB[i];
bj = false;
j = i;
} else {
//
HNE[j].setNext(HNB[i]);
j = i;
}
}
}
HNE[j].setNext(null);
return headtemp;
}
}
/**
*
* @author Administrator
*
*/
class Struts {
private int data;
private Struts next;
Struts() {
next = null;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Struts getNext() {
return next;
}
public void setNext(Struts next) {
this.next = next;
}
}
配列構造の実現package ;
import java.util.ArrayList;
import java.util.List;
public class BasicSort {
public void sort(int [] array){
int max = 0;//
for(int i = 0;i0){
max = max/10;
times++;
}
List queue = new ArrayList();//
for(int i = 0;i<10;i++){
ArrayList q = new ArrayList<>();
queue.add(q);
}
for(int i = 0;i0){
ArrayList q = queue.get(j);//
array[count] = q.get(0);
q.remove(0);
count++;
}
}
}
}
public static void main(String[] args){
BasicSort basicSort = new BasicSort();
int [] a = {136,2,6,8,9,2,8,11,23,56,34,90,89,29,145,209,320,78,3};
basicSort.sort(a);
for(int n:a){
System.out.print(" "+n);
}
}
}