排序算法

本文介绍了一个使用Java实现的排序算法演示程序,通过图形界面展示了不同排序算法(如冒泡排序、简单选择排序、选择排序和快速排序)的效果。程序中定义了自定义面板类用于绘制线条并直观展示排序过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//修饰类本身的只有public/缺省/final/abstract四种
interface I{
 public void g();
}

class A{
 int i;
 
 public I f(){
  final int j = 10;
  class B implements I{
   public B(){
    System.out.println (j);
   }
   
   public void g(){
    System.out.println (j);
   }
  }
  return new B();
 }
}


public class InnerClass{
 public static void main(String[] args){
     //A a = new A();
     //A.B ab = a.new B();
     
     //new A.B();
  A a = new A();
  I imps = a.f();
    }
}

 

 

 

 

 

import java.util.*;

public class Demo2{
 public static void prt(Collection c){
  Iterator itr = c.iterator();
  while (itr.hasNext())
   System.out.println (itr.next());
 }
 
 public static void main(String[] args){
     HashMap map = new HashMap();
     
     map.put("A",1);
     map.put(1,"A");
     map.put("A",2);
     map.put("B",1);
     
     //Collection = map.keySet();
     Set s = map.keySet();
     Collection c = map.values();
     prt(s);
     prt(c);
  
  Set key_values = map.entrySet();     
  Iterator itr = key_values.iterator();
  while (itr.hasNext()){
   Map.Entry me = (Map.Entry)itr.next();
   System.out.print (me.getKey()+"\t");
   System.out.println (me.getValue());
   
  }
     
    }
}

 

 

 

 

 

 

 -----------------------------------------------------------------------------------------------------------------------------------

//34    76     2     45    128   1   98   4
import java.io.*;

class T{
    int value;  
    int time = 0;
   
    public T(int value){
     this.value = value;
    }
   
    public String toString(){
     return "value"+value+",time="+time;
    }
}

public class Demo1{
 
 static T[] arr= new T[9];
 static int[] temp={0,34,76,2,45,128,1,98,4};   
 
 public static boolean search(int v){   
  for (int i = 1; i<arr.length; i++) {
   if (arr[i].value == v){
    arr[i].time++;
    if (i==1 || arr[i-1].time>arr[i].time)
     return true;
    else{
     arr[0] = arr[i];  
     int j = i-1;
     for (; j>=1 && arr[j].time<=arr[0].time; j--)
      arr[j+1] = arr[j];
     arr[j+1] = arr[0];
     return true;     
    }     
   }
  }
  return false;
 }
 
 public static void prt(){
  for (int i = 1; i<arr.length; i++)
   System.out.println (arr[i]);
 }
 


 public static void main(String[] args)
  throws Exception{
  for (int i = 1; i<arr.length; i++)       
   arr[i] = new T(temp[i]); 
  
  prt();
  BufferedReader in =
   new BufferedReader(
    new InputStreamReader(
     System.in));
   
  String input = null;
  while (true){
   input = in.readLine();
   if (input==null || input.equals("end")) break;
   search(Integer.parseInt(input));
   prt();
  }
    
  in.close();
  
   
    }
}
/*把下标为0的位置作为哨兵

1.如果找不到,则提示错误
2.如果找到了,则要看被找到的元素的位置
     (1)如果该位置下标为1,则不用移动
     (2)如果该位置前面的元素访问频率更高,则也不用移动
     (3)如果(1)(2)条件不符合,则移动元素到适当的位置
*/
   
     ======================================

package wood;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
class MyPanel extends JPanel
{
 private int x;
 public MyPanel(int x)
 {
     this.x = x;
  this.repaint();
  this.setSize(100,1);
 }
 public void paintComponent(Graphics g)
 {
  super.paintComponent(g);
  g.setColor(Color.red);
  g.drawLine(0,0,x,0);
 
 }
 public int getLen()
 {
  return this.x;
 }
 public void setLen(int x)
 {
  this.x = x;
 }
}
class MyFrame extends JFrame
{
  private MyPanel []mp;
  private MyPanel []mp1;
  private MyPanel []mp2;
  private MyPanel []mp3;
  private JButton btn = new JButton("开始");
  private JButton btn1 = new JButton("重试");
  private static Random rd = new Random();
  private JPanel title = new JPanel();
  private JPanel jpContainer = new JPanel();
  private JPanel jp = new JPanel();
  private JPanel jp1 = new JPanel();
  private JPanel jp2 = new JPanel();
  private JPanel jp3 = new JPanel();
  private JPanel jp4 = new JPanel();
  private static Sorter[] sorter= new Sorter[4];
        public MyFrame(String title)
        { 
            super(title);
            this.init();
            btn.addActionListener(new ActionListener()
            {
             public void actionPerformed(ActionEvent e)
             {
              int i=0;
               sorter[i++] =new Sorter(new BubbleSort(mp)) ;
                    sorter[i++] =new Sorter(new SimpleSelectSort(mp1));
              sorter[i++] =new Sorter(new SelectSort(mp2));
              sorter[i++] =new Sorter(new QuickSort(mp3));
              for(int j=0;j<4;j++)
              {
               sorter[j].sort();
              }
              
             }
            }
            );
            btn1.addActionListener(new ActionListener()
            {
             public void actionPerformed(ActionEvent e)
             {
              for(int j=0;j<2;j++)
              {
               sorter[j] = null;
              }
                    for(int i =0;i<100;i++)
                    {
                     int temp = rd.nextInt(200);
                     mp[i].setLen(temp);
                     mp1[i].setLen(temp);
                     mp2[i].setLen(temp);
                     mp3[i].setLen(temp);
                     mp[i].repaint();
                     mp1[i].repaint();
                     mp2[i].repaint();
                     mp3[i].repaint();
                    }
              System.out.println ("ok-------->");
             }
            }
            );
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setSize(new Dimension(850,500));
            this.setVisible(true);
        }
        public void init()
        {
         Container c = this.getContentPane();
            c.setLayout(new BorderLayout(1,3));
            c.add(title,BorderLayout.NORTH);
            JLabel jl1 = new JLabel("冒泡法排序");
            JLabel jl2 = new JLabel("简单选择法");
            JLabel jl3 = new JLabel("选择法排序");
            JLabel jl4 = new JLabel("快速排序法");
            title.setLayout(new GridLayout(1,4));
            title.add(jl1);title.add(jl2);title.add(jl3);title.add(jl4);
           
           
            c.add(jpContainer,BorderLayout.CENTER);
            this.jpContainer.setLayout(new GridLayout(1,4));
            jp.setLayout(new GridLayout(100,1));
            jpContainer.add(jp);
            mp = new MyPanel[100];
            for(int i=0;i<100;i++)
            {
             mp[i] = new MyPanel(rd.nextInt(200));
             
             jp.add(mp[i]);
             
            }
            jpContainer.add(jp1);
            jp1.setLayout(new GridLayout(100,1));
            mp1 = new MyPanel[100];
            for(int i=0;i<100;i++)
            {
             mp1[i] = new MyPanel(mp[i].getLen());
             jp1.add(mp1[i]);
            }
            jpContainer.add(jp2);
            jp2.setLayout(new GridLayout(100,1));
            mp2 = new MyPanel[100];
            for(int i=0;i<100;i++)
            {
             mp2[i] = new MyPanel(mp[i].getLen());
             jp2.add(mp2[i]);
            }
            jpContainer.add(jp3);
            jp3.setLayout(new GridLayout(100,1));
            mp3 = new MyPanel[100];
            for(int i=0;i<100;i++)
            {
             mp3[i] = new MyPanel(mp[i].getLen());
             jp3.add(mp3[i]);
            }
            c.add(jp4,BorderLayout.SOUTH);
            jp4.add(btn);
            jp4.add(btn1);
        }
        public static void main(String[] args)
        {
               MyFrame mf = new MyFrame("MySort");
        }
}

     
     
     
     
     
     
     
     
     
     
    

 ================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值