public class Sort
{
//选择排序
public static void selectSort(char[] data)
{
int i,j,mx;
char temp;
for(i = 0; i<datalength-1; i++)
{
mx = i ;
for(j = i+1 ; j<datalength; j++)
{
if(data[mx] < data[j])
mx = j ;
}
temp = data[i];
data[i] = data[mx];
data[mx] = temp;
}
}
//插入排序
public static void insertSort(char[] data)
{
int i,j,mx;
char key ;
for(i=1; i < datalength -1; i++)
{
key = data[i];
mx = i;
for(j= i-1;j>0;j--)
{
if(key>data[j])
{
data[j+1] = data[j];
mx = j ;
}
}
data[mx]=key;
}
}
//显示数据
public static void showData(char[] data)
{
int i;
for(i=0; i<datalength; i++)
Systemoutprint(data[i]+" ");
Systemoutprintln();
}
public static void main(String[] args)
{
char[] data = {'9','8','7','6','5','4','3','2','1'};
SortselectSort(data);
SortshowData(data);
SortinsertSort(data);
SortshowData(data);
}
}
这个程序还不够你的要求,你可以再修改修改
1 int high = Alength - 1;
import javautilArrays;
public class SortAndSearch
{
private int[] A;
public SortAndSearch ( int[] arry )
{
int i = 0;
A = new int[arrylength];
for ( int value : arry )
{
A[i++] = value;
}
}
public void Sort ( int[] a )
{
int i, j, temp;
for ( i = 0; i < alength - 1; i++ )
{
for ( j = i + 1; j < alength; j++ )
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
Systemoutprint (a[i] + ",");
}
temp = alength - 1;
Systemoutprintln (a[temp]);
}
public int Search ( int x )
{
int low = 0;
int high = Alength - 1;
while (low <= high)
{
int mid = ( low + high ) / 2;
if (x > A[mid])
{
low = mid + 1;
}
else if (x < A[mid])
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
public static void main ( String[] args )
{
int[] a = { 28, 39, 49, 78, 23 };
SortAndSearch sas = new SortAndSearch (a);
Systemoutprintln ("排序前的数组为:28,39,49,78,23");
Systemoutprint ("选择排序后的数组为:");
sasSort (a);
Systemoutprintln (ArraystoString (sasA));
int n = 78;
Systemoutprintf ("折半查找%d索引是%d", n, sasSearch (n));
}
}
对汉字是按拼音排序的,示例:import javautil;
import javatext;public class Test{
public static void main(String args[]) {
//Comparator cmp = (RuleBasedCollator)javatextCollatorgetInstance(javautilLocaleCHINA);//try testing various locales
Comparator cmp = javatextCollatorgetInstance(javautilLocaleCHINA);//try testing various locales
String[] arr = {"张三", "李四", "张", "王"};
javautilArrayssort(arr, cmp);
for (int i = 0; i < arrlength; i++)
Systemoutprintln(arr[i]);
Comparator cmp2 = javatextCollatorgetInstance();//try default locale
String[] arr2 = {"我", "是", "草", "吗"};
javautilArrayssort(arr2, cmp2);
for (int i = 0; i < arr2length; i++)
Systemoutprintln(arr2[i]);
}
}
给你一个吧,实现的是可以达到小,小到大排序!
package omblingorder;
import javaioBufferedReader;
import javaioIOException;
import javaioInputStreamReader;
public class OrderNumber {
public OrderNumber() {
}
private String input(){
Systemoutprintln("请输入一系列整数,中间用逗号(英文输入法下面)隔开:");
BufferedReader buf = new BufferedReader(new InputStreamReader(Systemin));
String value = "";
try {
value = bufreadLine();
if (valueindexOf(",") == -1){
Systemoutprintln("你输入的数字格式有误!");
return null;
}
} catch (IOException e) {
eprintStackTrace();
}
return value;
}
private String orderType(){
String type = "";
Systemoutprintln("请输入排列方式,大到小(lts),小到大(stl):");
BufferedReader buf = new BufferedReader(new InputStreamReader(Systemin));
try {
type = bufreadLine();
Systemoutprintln(type);
if (null == type || ""equals(typetrim()) ){
Systemoutprintln("请重新输入排序方式!");
return null;
}
} catch (IOException e) {
eprintStackTrace();
}
return type;
}
private int[] ChangeInt(String value){
String[] values = valuesplit(",");
int size = valueslength;
int[] int_values = new int[size];
for (int i=0;i<size;i++){
int_values[i] = IntegerparseInt(values[i]);
}
return int_values;
}
private void ltsOrder(int[] values){
int temp;
for (int i=0;i<valueslength;i++){
for (int j=i;j<valueslength;j++){
if (values[i] < values[j]){
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
thisprint(values);
}
private void stlOrder(int[] values){
int temp;
for (int i=0;i<valueslength;i++){
for (int j=i;j<valueslength;j++){
if (values[i] > values[j]){
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
thisprint(values);
}
private void print(int[] values){
for (int i:values){
Systemoutprint(i+"\t");
}
}
public static void main(String[] args) {
OrderNumber order = new OrderNumber();
String value = orderinput();
while (value == null){
value = orderinput();
}
String orderType = orderorderType();
while (orderType == null){
orderType = orderorderType();
}
if ("lts"equals(orderType)){
orderltsOrder(orderChangeInt(value));
}else if ("stl"equals(orderType)){
orderstlOrder(orderChangeInt(value));
}else {
Systemoutprintln("很抱歉,由于你输入的排序方式不正确,程序退出!");
}
}
}
以上就是关于排序算法 java编程全部的内容,包括:排序算法 java编程、Java编写程序:排序和查找、求一个JAVA的汉字排序程序。。。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)