java算法:排序实现
排序最基本规则:先比较,再对数据项进行排序。
例1:数据项接口
- interfaceItem{
-
booleanless(Itemv);
- }
interface Item{
boolean less(Item v);
}
例2:排序方法类
- classSort{
-
staticbooleanless(Itemv,Itemw){
-
returnv.less(w);
- }
-
staticvoidexch(Item[]a,inti,intj){
- Itemt=a[i];
- a[i]=a[j];
- a[j]=t;
- }
-
staticvoidcomExch(Item[]a,inti,intj){
-
if(less(a[j],a[i])){
- exch(a,i,j);
- }
-
voidsort(Item[]a,intl,intr){
- example(a,l,r);
- }
-
staticvoidexample(Item[]a,intl,intr){
-
for(inti=l;i<r;i++){
-
for(intj=i;j>l;j--){
-
compExch(a,j-1,j);
- }
- }
- }
- }
class Sort{
static boolean less(Item v, Item w){
return v.less(w);
}
static void exch(Item [] a, int i, int j){
Item t = a[i];
a[i] = a[j];
a[j] = t;
}
static void comExch(Item [] a, int i, int j){
if(less(a[j], a[i])){
exch(a, i, j);
}
void sort(Item [] a, int l, int r){
example(a, l, r);
}
static void example(Item [] a, int l, int r){
for(int i = l; i < r; i++){
for(int j = i; j > l; j--){
compExch(a, j - 1, j);
}
}
}
}
数据项ADT的接口:数据项,或要排序的一般对象;数据项数组。
例3:数据项ADT接口
- classMyItemimplementsItem{
-
publicbooleanless(Item)
-
voidread()
-
voidrand()
- pubicStringtoString()
- }
class MyItem implements Item{
public boolean less(Item)
void read()
void rand()
pubic String toString()
}
排序算法不仅对数据项,也对数据项数组起作用。
例4:可排序的数组ADT接口
- classSArray{
-
SArray(int)
-
voidrand()
-
voidread()
-
voidshow(int,int)
-
voidsort(int,int)
- }
class SArray{
SArray(int)
void rand()
void read()
void show(int, int)
void sort(int, int)
}
例5:可排序数组的排序驱动程序
- classArraySort{
-
publicstaticvoidmain(String[]args){
-
intN=100;
-
SArraysa=newSArray(100);
- sa.rand();
-
sa.sort(0,N-1);
-
sa.show(0,N-1);
- }
- }
class ArraySort{
public static void main(String [] args){
int N = 100;
SArray sa = new SArray(100);
sa.rand();
sa.sort(0, N - 1);
sa.show(0, N - 1);
}
}
该程序表明:可以定义一个这样的计算而不需要涉及要排序的数据类型。
模块化的组织方式允许用别的实现来代替,这取决于该应用。如,当对大型数组测试排序时,可能会使用只输出部分数组的show的实现。
例6:可排序数组ADT的样本实现
- classMyArray{
-
privateItem[]a;
-
privateintN;
-
MyArray(intN){
-
this.N=N;
-
a=newItem[N];
-
for(inti=0;i<N;i++){
-
a[i]=newItem();
- }
- }
-
voidrand(){
-
for(inti=0;i<N;i++){
- a[i].rand();
- }
- }
-
voidread(){
-
for(inti=0;i<N;i++){
-
if(!In.empty()){
- a[i].read();
- }
- }
- }
-
voidshow(intl,intr){
-
for(inti=l;i<r;i++){
-
System.out.println(a[i]+"");
- }
- }
-
voidsort(intl,intr){
- Sort.sort(a,l,r);
- }
- }
class MyArray{
private Item[] a;
private int N;
MyArray(int N){
this.N = N;
a = new Item[N];
for(int i = 0; i < N; i++){
a[i] = new Item();
}
}
void rand(){
for(int i = 0; i < N; i++){
a[i].rand();
}
}
void read(){
for(int i = 0; i < N; i++){
if(!In.empty()){
a[i].read();
}
}
}
void show(int l, int r){
for(int i = l; i < r; i++){
System.out.println(a[i] + " ");
}
}
void sort(int l, int r){
Sort.sort(a, l, r);
}
}
对不同类型数据项进行排序的ADT实现。
例7:整数数据项的ADT实现
- classMyItemimplementsItem{
-
privateintkey;
-
publicbooleanless(Itemv){
-
returnkey<((Item)v).key;
- }
-
voidread(){
- key=In.getInt();
- }
-
voidrand(){
-
key=(int)(1000*Math.random());
- }
-
publicStringtoString(){
-
returnkey+"";
- }
- }
class MyItem implements Item{
private int key;
public boolean less(Item v){
return key < ((Item)v).key;
}
void read(){
key = In.getInt();
}
void rand(){
key = (int)(1000 * Math.random());
}
public String toString(){
return key + "";
}
}
例8:样本记录类
- classRecord{
-
intid;
-
doublebalance;
- Stringwho;
-
staticintSortKeyField=0;
-
publicStringtoString(){
-
returnid+""+balance+""+who;
- }
- }
class Record{
int id;
double balance;
String who;
static int SortKeyField = 0;
public String toString(){
return id + " " + balance + " " + who;
}
}
例9:记录项的ADT实现
- classMyItemextendsRecordimplementsItem{
-
publicbooleanless(Itemv){
- MyItemr=(MyItem)v;
-
switch(SortKeyField){
-
case2:returnwho.compareTo(r.who)<0;
-
case1:returnbalance<r.balance;
-
default:returnid<r.id;
- }
- }
-
voidread(){
- id=In.getInt();
- balance=In.getDouble();
- who=In.getString();
- }
- }
class MyItem extends Record implements Item{
public boolean less(Item v){
MyItem r = (MyItem) v;
switch(SortKeyField){
case 2: return who.compareTo(r.who) < 0;
case 1: return balance < r.balance;
default: return id < r.id;
}
}
void read(){
id = In.getInt();
balance = In.getDouble();
who = In.getString();
}
}
上述方法在经典文献中被称为指针排序。
例10:字符串数据项的ADT实现
- classMyItemimplementsItem{
- Stringkey;
-
publicboolean(Itemv){
-
returnkey.compareTo(((MyItem)v).key)<0;
- }
-
voidread(){
- key=In.getString();
- }
-
voidrand(){
-
inta=(int)('a');
-
key="";
-
for(inti=0;i<1+9*Math.random();i++){
-
key+=(char)(a+26*Math.random();
- }
- }
-
publicStringtoString(){
-
returnkey;
- }
- }
class MyItem implements Item{
String key;
public boolean(Item v){
return key.compareTo(((MyItem)v).key) < 0;
}
void read(){
key = In.getString();
}
void rand(){
int a = (int)('a');
key = "";
for(int i = 0; i < 1 + 9*Math.random(); i++){
key += (char)(a + 26*Math.random();
}
}
public String toString(){
return key;
}
}
说明了在java中对关键字使用String域直接实现String对象的MyItemADT。
java方法有很多好处。在排序情况下,使用引用避免对排序的数组直接进行操作。即时是只读文件,也能对它进行“排序”。而且,通过使用多重引用数组,能对一个数据体用两种不同的排序表示法来表示。这种不改变数据就能对数据进行操作的灵活性在许多应用中都十分有用的,同时,使用引用能节省移动整个记录的开销。
分享到:
相关推荐
Java排序算法实现 Java排序算法实现 Java排序算法实现
实现合并排序,插入排序,希尔排序,快速排序,冒泡排序,桶排序算法的java实现。
java可运行排序算法:①插入排序、②冒泡排序、③选择排序、④学生学号按照成绩高低排序的一个简单实例。在java工程项目的源文件src中建立Array包,可运行这四个.java文件,便于对java中的排序算法及数组结构进一步...
Java排序算法实现资源 这个资源是关于Java中排序算法实现的简单示例。排序算法是计算机科学中的基础概念,用于按升序或降序排列数据集。这里提供了两种常见的排序算法实现:冒泡排序和选择排序。 冒泡排序(Bubble ...
JAVA排序算法: 直接插入,希尔排序,选择排序,堆排序,冒泡排序,快速排序,归并排序,包括算法的详细介绍,以及对几种算法的详细测试
算法分类:排序算法(如冒泡排序、快速排序、归并排序),查找算法(如顺序查找、二分查找、哈希查找),图论算法(如Dijkstra最短路径算法、Floyd-Warshall算法、Prim最小生成树算法),动态规划,贪心算法,回溯法...
java 算法:包括数组,哈希表,队列,栈,链表(双端,单向,双向),二叉树(普通二叉树,哈夫曼树,二叉查找树,平衡二叉树,二叉线索树),图这些数据结构的实现以及多种排序算法和其他一些算法的实现(递归,二...
Java_Algorithm(Java算法集合) 学习算法是为了什么? 1、应对大型IT公司的算法面试题; 2、IDE即对编译算法的封装; 3、搜索引擎中对几千、几亿数据进行优劣排序; 4、游戏对算法的引用是非常丰富的; 5、算法对...
Java数据挖掘18大算法实现和10大常见排序算法以及其他相关经典DM算法集合。 18大数据挖掘的经典算法以及代码实现,涉及到了决策分类,聚类,链接挖掘,关联挖掘,模式挖掘等等方面,后面都是相应算法的文章,希望能够...
* 冒泡排序: * 每次在无序队列里将相邻两个数一次进行比较, * 将小数调到前面,逐次比较,直至将最大的数移到 * 最后。将剩下的N-1个数继续比较,将次大数移至 * 倒数第二位。
java实现的常用的几种基本排序算法,插入、交换、选择、归并
问题描述:实现合并排序,插入排序,希尔排序,快速排序,冒泡排序,桶排序算法 实验要求: A. 在随机产生的空间大小分别为 N = 10, 1000,10000,100000 的排序样本(取值为[0,1])上测试以上算法。 B.结果输出...
Java经典算法 ,各种排序算法 老掉牙 河內塔 費式數列 巴斯卡三角形 三色棋 老鼠走迷官(一) 老鼠走迷官(二) 騎士走棋盤 八個皇后 八枚銀幣 生命遊戲 字串核對 雙色、三色河內塔 背包問題(Knapsack...
Java实现的常见排序算法, Java实现的常见排序算法, Java实现的常见排序算法, Java实现的常见排序算法, Java实现的常见排序算法,
用java对常用排序算法进行分析与实现.包含: 插入排序 直接插入排序、希尔排序 • 选择排序 简单选择排序、堆排序 • 交换排序 冒泡排序、快速排序 • 归并排序 • 基数排序
java排序算法使用及场景说明 文档后面有一些别人的链接,多在google上搜索Java排序算法,及维基百科上面也有很全的算法介绍。
详细解释了快速排序的java实现.里面有代码,还有注释说明
基数排序算法 java实现 还有基数排序的原理文档
在这个教程中,我们将深入研究插入排序的原理,并提供一个Java示例来演示如何实现它。不管您是初学者还是有经验的Java开发者,通过学习这个算法,您将了解一种排序方法,有助于提高您的算法理解和编程技能。 插入...