Interesting array

Today I learned about aspects of the Java array which, it is C, C ++, but there's an array of very different ah. Let me introduce the knowledge and experience of their own after learning this aspect of it.
Statement Java among the array of open:
Array Type [] array name = new array type [array length]; (one-dimensional array)
for example: int [] data = new int [10];
data type object array [] [] = new data type [row number] [column number]; (two-dimensional array)
:; data such as int [] [] = new int [. 8] [. 7]
: or may be
an array element name = {array type array [] };
For example: int [] data = {1,2,2,3,4,5 }; // its length is 6;
data type object array [] [] = {{...}, {...}, ... { }};
If we say that we have defined an array of length 6, but we want to go into seven elements, this time in order to avoid array bounds, we can re-open a new array: data = new int [7] ;
above we mentioned the type array, the array, which has several types of what? I tell you, no, the teacher said, the array has eight types:. Int, double, char, byte, long, boolean, float, short
then, we have to sort the array which aspects of learning, feeling the principles and C ++ among the same principle, the so-called original aim, is not it. Let's take a little talk about the bubble sort and quick sort well known now.
Bubble Act Demo:
very simple, very few, it is understood interview when asked more used!

The sequence of all the elements pairwise comparison, the largest on the last.

The remaining elements in the sequence of all pairwise comparisons, the largest on the final surface.

The second step is repeated until only a few.
Set the number of cycles.

Set start comparing the median, and ending digits.

Pairwise comparison, to the front to the minimum.

Repeat steps 2 and 3, until the number of cycles completed.

int[] a={10,9,4,6,2,8,3,7,1,5};
		String act = e.getActionCommand();
		if("冒泡排序法".equals(act))
		for(int i=0;i<a.length;i++)
			for(int j=i+1;j<a.length;j++)
			{
				if(a[i]>a[j])
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
				}......

Fast sorting method:
by ordering the trip records to be sorted into two independent parts, wherein the key part of the record keyword portion smaller than those of the other, the two parts respectively sequencing continues until the entire sequence of ordered.

A quick example of the sort:

(A) a trip sorting process:

The whole process (b) ordered

Seen as a whole array of sequences, the zeroth position seen axis, and the last one than if it is smaller than the exchange, it is large than no treatment; and then exchanged after the end of the line ratio smaller than it little exchange, than he exchanged. In this way the cycle, a trip sorting is completed, the left is smaller than the central axis of the right is larger than the central axis, and then use divide and conquer, each of these two independent sort the array.

public   int getMiddle(int[] a, int low,int high)
    {
		int start = low;
		int end = high;
        int temp = a[start]; //数组的第一个作为中轴
        while(start< end)
        {
        while(start < end && a[end] > temp)
        {
            end--;
        }
        a[start] = a[end];//比中轴小的记录移到低端
         while(start < end && a[start] < temp)
        {
            start++;
        }
        a[end] = a[start] ; //比中轴大的记录移到高端
        }
         a[start] = temp ; //中轴记录到尾
         return start;
         }
         public void QuickSort(int[] a,int low,int high)
	    {
	        if(low < high)
	        {
	       int middle = getMiddle(a,low,high); //将numbers数组进行一分为二
	       QuickSort(a, low, middle-1);   //对低字段表进行递归排序
	       QuickSort(a, middle+1, high); //对高字段表进行递归排序
	        }
	    
	    }

Then we can set ourselves a drawing board with these two dynamic image sorting process visual representation out, this is I wrote a regular demo code:

import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;


public class Sort {

	public void showUI(){
		JFrame jfr = new JFrame();
		jfr.setSize(1000,800);
		jfr.setTitle("排序图像演示");
		jfr.setLocationRelativeTo(null);
		jfr.setDefaultCloseOperation(3);
		jfr.setLayout(new FlowLayout());
		DrawListener drawl = new DrawListener();
		JButton jbt = new JButton("冒泡排序法");
		jfr.add(jbt);
		jbt.addActionListener(drawl);
		JButton jbt1 = new JButton("快速排序法");
		jfr.add(jbt1);
		jbt1.addActionListener(drawl);
		jfr.setVisible(true);
		
		Graphics g =jfr.getGraphics();
		drawl.g=g;
	}
	public static void main(String[] agrs){
		Sort s =new Sort();
		s.showUI();
	}
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DrawListener implements ActionListener {
	
	Graphics g;
	public   int getMiddle(int[] a, int low,int high)
    {
		int start = low;
		int end = high;
        int temp = a[start]; //数组的第一个作为中轴
        while(start< end)
        {
        while(start < end && a[end] > temp)
        {
            end--;
        }
        a[start] = a[end];//比中轴小的记录移到低端
        g.setColor(Color.WHITE);
		g.fillRect(0,0,2000,1000);
		for(int k=0;k<a.length;k++)
		{
			
			Color c =new Color(0,a[k]*10,0);
			g.setColor(c);
			g.drawString(""+a[k],(k*40)+40, 150);
			
			g.fillRect(k*40+40,200,20,20*a[k]);
		}
		try {
			Thread.sleep(400);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        while(start < end && a[start] < temp)
        {
            start++;
        }
        a[end] = a[start] ; //比中轴大的记录移到高端
        g.setColor(Color.WHITE);
		g.fillRect(0,0,2000,1000);
		for(int k=0;k<a.length;k++)
		{
			
			Color c =new Color(0,a[k]*10,0);
			g.setColor(c);
			g.drawString(""+a[k],(k*40)+40, 150);
			
			g.fillRect(k*40+40,200,20,20*a[k]);
		}
		try {
			Thread.sleep(700);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        }
        a[start] = temp ; //中轴记录到尾
        g.setColor(Color.WHITE);
		g.fillRect(0,0,2000,1000);
		for(int k=0;k<a.length;k++)
		{
			
			Color c =new Color(0,a[k]*10,0);
			g.setColor(c);
			g.drawString(""+a[k],(k*40)+40, 150);
			
			g.fillRect(k*40+40,200,20,20*a[k]);
		}
		try {
			Thread.sleep(700);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        return start ; // 返回中轴的位置
    }
	 public void QuickSort(int[] a,int low,int high)
	    {
	        if(low < high)
	        {
	       int middle = getMiddle(a,low,high); //将numbers数组进行一分为二
	       QuickSort(a, low, middle-1);   //对低字段表进行递归排序
	       QuickSort(a, middle+1, high); //对高字段表进行递归排序
	        }
	    
	    }
	/*/public void FastSort(int[] a,int low,int high){
		int start = low;
		int end =high;
		int temp=a[low];
		while(start<end){
			while(start<end&&a[end]>temp){
				end--;
			}
			a[start]=a[end];
			while(start<end&&a[start]<temp){
				start++;
			}
			a[end]=a[start];
			a[start]=temp;
			if(start<end){
				FastSort(a,low,start-1);
				FastSort(a,start+1,high);
			}
		}
		
	}/*/
	public void actionPerformed(ActionEvent e){
		int[] a={10,9,4,6,2,8,3,7,1,5};
		String act = e.getActionCommand();
		if("冒泡排序法".equals(act))
		for(int i=0;i<a.length;i++)
			for(int j=i+1;j<a.length;j++)
			{
				if(a[i]>a[j])
			{
				int temp=a[i];
				a[i]=a[j];
				a[j]=temp;
				g.setColor(Color.WHITE);
				g.fillRect(0,0,2000,1000);
				for(int k=0;k<a.length;k++)
				{
					
					Color c =new Color(0,a[k]*10,0);
					g.setColor(c);
					g.drawString(""+a[k],(k*40)+40, 150);
					
					g.fillRect(k*40+40,200,20,20*a[k]);
				}
				try {
					Thread.sleep(400);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
				
			}
		if("快速排序法".equals(act)){
			QuickSort(a, 0, 9);
			for(int i=0;i<10;i++)
				System.out.println(" "+a[i]);
		}

}
}
Published 22 original articles · won praise 27 · views 2859

Guess you like

Origin blog.csdn.net/weixin_44346470/article/details/88715010