給你介紹4種排序方法及源碼,供參考
成都創(chuàng)新互聯(lián)專注于尼金平網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供尼金平營(yíng)銷型網(wǎng)站建設(shè),尼金平網(wǎng)站制作、尼金平網(wǎng)頁(yè)設(shè)計(jì)、尼金平網(wǎng)站官網(wǎng)定制、微信小程序定制開(kāi)發(fā)服務(wù),打造尼金平網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供尼金平網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
1.冒泡排序
主要思路: 從前往后依次交換兩個(gè)相鄰的元素,大的交換到后面,這樣每次大的數(shù)據(jù)就到后面,每一次遍歷,最大的數(shù)據(jù)到達(dá)最后面,時(shí)間復(fù)雜度是O(n^2)。
public?static?void?bubbleSort(int[]?arr){
for(int?i?=0;?i??arr.length?-?1;?i++){
for(int?j=0;?j??arr.length-1;?j++){
if(arr[j]??arr[j+1]){
arr[j]?=?arr[j]^arr[j+1];
arr[j+1]?=?arr[j]^arr[j+1];
arr[j]?=?arr[j]^arr[j+1];
}
}
}
}
2.選擇排序
主要思路:每次遍歷序列,從中選取最小的元素放到最前面,n次選擇后,前面就都是最小元素的排列了,時(shí)間復(fù)雜度是O(n^2)。
public?static?void?selectSort(int[]?arr){
for(int?i?=?0;?i?arr.length?-1;?i++){
for(int?j?=?i+1;?j??arr.length;?j++){
if(arr[j]??arr[i]){
arr[j]?=?arr[j]^arr[i];
arr[i]?=?arr[j]^arr[i];
arr[j]?=?arr[j]^arr[i];
}
}
}
}
3.插入排序
主要思路:使用了兩層嵌套循環(huán),逐個(gè)處理待排序的記錄。每個(gè)記錄與前面已經(jīng)排好序的記錄序列進(jìn)行比較,并將其插入到合適的位置,時(shí)間復(fù)雜度是O(n^2)。
public?static?void?insertionSort(int[]?arr){
int?j;
for(int?p?=?1;?p??arr.length;?p++){
int?temp?=?arr[p];???//保存要插入的數(shù)據(jù)
//將無(wú)序中的數(shù)和前面有序的數(shù)據(jù)相比,將比它大的數(shù),向后移動(dòng)
for(j=p;?j0??temp?arr[j-1];?j--){
arr[j]?=?arr[j-1];
}
//正確的位置設(shè)置成保存的數(shù)據(jù)
arr[j]?=?temp;
}
}
4.希爾排序
主要思路:用步長(zhǎng)分組,每個(gè)分組進(jìn)行插入排序,再慢慢減小步長(zhǎng),當(dāng)步長(zhǎng)為1的時(shí)候完成一次插入排序,? 希爾排序的時(shí)間復(fù)雜度是:O(nlogn)~O(n2),平均時(shí)間復(fù)雜度大致是O(n^1.5)
public?static?void?shellSort(int[]?arr){
int?j?;
for(int?gap?=?arr.length/2;?gap??0?;?gap/=2){
for(int?i?=?gap;?i??arr.length;?i++){
int?temp?=?arr[i];
for(j?=?i;?j=gap??temparr[j-gap];?j-=gap){
arr[j]?=?arr[j-gap];
}
arr[j]?=?temp;
}
}
}
public class TestStar {
public static void main(String[] args) {
String star = "*";
for (int i = 0; i 5; i++) {
if (i == 0) {
System.out.print(" " + star);
System.out.println();
}
if (i == 1) {
for (int z = 0; z 4; z++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 2) {
System.out.print(" ");
for (int x = 0; x 3; x++) {
System.out.print(" " + star);
}
System.out.println();
}
if (i == 3) {
for (int y = 0; y 2; y++) {
System.out.print(" " + star + " ");
}
}
}
}
}
是好使的 但是我沒(méi)找到畫(huà)五角星有什么規(guī)律(五角星好象不是正規(guī)圖形吧?)如果還有什么要求的話 補(bǔ)充問(wèn)題(如果是用*填充所有的東西 不包括 “ ”的話 我可以重新再給你寫(xiě)一個(gè))
學(xué)java的時(shí)候自己編的,很簡(jiǎn)單,能夠連續(xù)輸入計(jì)算式后進(jìn)行計(jì)算
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**簡(jiǎn)易計(jì)算器,能夠進(jìn)行簡(jiǎn)單的計(jì)算
*
* @see 2008.12.9
*/
public class CalculatorA
implements ActionListener{
private JFrame frame;
private JTextField field;
private JButton[] allButtons;
private JButton clearButton;
// private JButton backButton;
String result="";//保存結(jié)果
StringBuilder sb = new StringBuilder();//保存要進(jìn)行的計(jì)算式
int x = 0; //用來(lái)判斷上一次的事件類型
String str = "123+456-789*0.=/";
ArrayListString arrayList = new ArrayListString();//保存計(jì)算式,通過(guò)方法進(jìn)行運(yùn)算
public CalculatorA(){
frame = new JFrame("我的計(jì)算器v1.1");
frame.setLocation(300,300);
field = new JTextField(25);
allButtons = new JButton[16];
for(int i=0;iallButtons.length;i++){
allButtons[i]= new JButton(str.substring(i,i+1));
}
clearButton = new JButton("CLEAR");
// backButton = new JButton("——");
init();
setFondAndColor();
addEventHander();
}
public void init(){
frame.setLayout(new BorderLayout());
JPanel northPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel southPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(field);
for(int i=0;iallButtons.length;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
//southPanel.add(backButton);
frame.add(northPanel,BorderLayout.NORTH);
frame.add(centerPanel,BorderLayout.CENTER);
frame.add(southPanel,BorderLayout.SOUTH);
}
//設(shè)置輸入字體
public void setFondAndColor(){
field.setFont(new Font("宋體",Font.BOLD,24));
field.setBackground(new Color(100,200,200));
field.setForeground(Color.RED);
//設(shè)置字體從右起始
field.setHorizontalAlignment(JTextField.RIGHT);
}
public void showMi(){
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addEventHander(){
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(this);
// backButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();//取得當(dāng)前事件返回的值
if("0123456789.".indexOf(str)!=-1){
if(x == 0){ //當(dāng)x為0時(shí)表示還沒(méi)有進(jìn)行輸入
result=str;
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 1){
result = result +str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 2){
sb.delete(0,sb.length());
result = result+str;
sb.append(str);
field.setText(result);
x = 1;
}
else if(x == 3){
result = str;
sb.delete(0,sb.length());
arrayList.clear();
field.setText(str);
sb.append(str);
field.setText(str);
x = 1;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
result = str;
sb.append(str);
field.setText(str);
x = 1;
}
else{
result = result +str;
sb.append(str);
field.setText(result);
x = 1;
}
}
else if("+*-/".indexOf(str)!=-1){
if(x == 0){
field.setText("");
x = 2;
}
else if(x == 1){
result = result + str;
arrayList.add(sb.toString());
arrayList.add(str);
sb.append(str);
field.setText(result);
x = 2;
}
else if(x == 2){
x = 2;
}
else if(x == 3){
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result = result+str;
x = 2;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 2;
}
else{
field.setText(result+str);
arrayList.add(result);
arrayList.add(str);
result = result+str;
x = 2;
}
}
else if("=".equals(str)){
if(x == 0){
field.setText("0");
arrayList.clear();
result = "0";
x = 3;
}
else if(x == 1){
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("數(shù)據(jù)格式異常");
x = 0;
}
}
else if(x == 2){
field.setText("數(shù)據(jù)格式錯(cuò)誤.....");
arrayList.clear();
x = 0;
}
else if(x == 3){
field.setText(result);
x = 3;
}
else if(x == 4){
result ="";
sb.delete(0,sb.length());
arrayList.clear();
x = 3;
}
else {
try{
arrayList.add(sb.toString());
arrayList = getResult(arrayList);
result = arrayList.get(0);
field.setText(result);
arrayList.clear();
x = 3;
}catch(Exception e1){
field.setText("數(shù)據(jù)格式異常");
x = 0;
}
}
}
else if("CLEAR".equals(str)){
arrayList.clear();
field.setText("0");
arrayList.add("0");
x = 4;
}
else{
if(result.length()1){
result = result.substring(0,result.length()-1);
if(sb.length()0){
sb.delete(sb.length()-1,sb.length());
}
else {
sb.delete(0,1);
}
field.setText(result);
x = 5;
}
else{
result = "";
sb.delete(0,sb.length());
arrayList.clear();
field.setText("0");
x = 0;
}
}
}
public static ArrayListString getResult(ArrayListString list){
String res = null;
String[] s = {"/","*","-","+"};
int i=0;
if(list.size()1){
for(;is.length;){
if(s[i].equals("/")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))/Double.parseDouble(list.get(j+1)));
//本地的數(shù)據(jù)格式
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("*")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))*Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else if(s[i].equals("-")){
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))-Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getNumberInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
else {
for(int j=0;jlist.size();j++){
if(list.get(j).equals(s[i])){
res = Double.toString(Double.parseDouble(list.get(j-1))+Double.parseDouble(list.get(j+1)));
NumberFormat nf = NumberFormat.getInstance();
res = nf.format(Double.parseDouble(res));
res = getChange(res);
list.set(j-1,res);
list.remove(j);
list.remove(j);
getResult(list);
}
}
i++;
}
}
}
return list;
}
//對(duì)數(shù)字字符串進(jìn)行排除不必要符號(hào)
public static String getChange(String res){
String s_temp = "";
char[] c = new char[res.length()];
for(int k=0;kc.length;k++){
c[k] = res.charAt(k);
}
for(int k=0;kc.length;k++){
if((c[k]= '0' c[k]= '9')|| c[k] == '.'){
s_temp += c[k];
}
}
return s_temp;
}
public static void main(String[] args){
new CalculatorA().showMi();
}
}
最簡(jiǎn)單的java代碼肯定就是這個(gè)了,如下:
public class MyFirstApp
{
public static void main(String[] args)
{
System.out.print("Hello world");
}
}
“hello world”就是應(yīng)該是所有學(xué)java的新手看的第一個(gè)代碼了。如果是零基礎(chǔ)的新手朋友們可以來(lái)我們的java實(shí)驗(yàn)班試聽(tīng),有免費(fèi)的試聽(tīng)課程幫助學(xué)習(xí)java必備基礎(chǔ)知識(shí),有助教老師為零基礎(chǔ)的人提供個(gè)人學(xué)習(xí)方案,學(xué)習(xí)完成后有考評(píng)團(tuán)進(jìn)行專業(yè)測(cè)試,幫助測(cè)評(píng)學(xué)員是否適合繼續(xù)學(xué)習(xí)java,15天內(nèi)免費(fèi)幫助來(lái)報(bào)名體驗(yàn)實(shí)驗(yàn)班的新手快速入門java,更好的學(xué)習(xí)java!
public
static
int
sumSome(int
pos,int
max){
int[]
args
=
{4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8,5,4,1,5,1,8,0,6,4,3,5,8};
int
total
=
0;
/**
*
數(shù)組下標(biāo)從0開(kāi)始,所以(pos
-
1)
*
每次獲取下標(biāo)為
pos+pos的值,所以
i
+=
pos
*/
for(int
i
=
(pos
-
1);
i
args.length;
i
+=
pos){
total
+=
args[i];
if(total
max){
break;
}
}
return
total;
}
package com.zpp;public class Charge {
public static void main(String [] args) {
if(args.length ==0) {
System.out.println("parameter error!");
System.out.println("java com.zpp.Charge [int]");
return;
}
int min = Integer.parseInt(args[0]);
double money = 0.0;
if (min = 0) {
money =0.0;
System.out.println("not money");
} else if (min = 60) {
money = 2.0;
} else {
money = 2.0 + (min - 60) * 0.01;
}
System.out.println("please pay: " + money);
}
} 編譯:javac -d . Charge.java運(yùn)行:java com.zpp.Charge 111