1.冒泡排序

(1)基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。

(2)实例:

冒泡排序、插入排序、选择排序的简单了解_插入排序

package com.hanchao.sort;
/***********************
 * 冒泡排序
 * @author:han   
 * @version:1.0       
 * @created:2013-10-14   
 ***********************
 */
public class BubbleSort {
    public static void main(String[] args) {
                        
        int[] a = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
        int temp = 0;
                        
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a.length - 1 - i; j++) {
                if(a[j] > a[j+1]) {
                    temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
                        
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + ",");
        }
                    
    }
}



2.简单选择排序

(1)基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;

然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。

(2)实例:

冒泡排序、插入排序、选择排序的简单了解_冒泡排序_02

package com.hanchao.sort;
/***********************
 * 选择排序
 * @author:han   
 * @version:1.0       
 * @created:2013-10-14   
 ***********************
 */
public class SelectSort {
    public static void main(String[] args) {
                 
        int[] a = {1,54,6,3,78,34,12,45};
        int position  = 0;
                 
        for (int i = 0; i < a.length; i++) {
            int j = i + 1;
            position = i;
            int temp = a[i];
                     
            for (; j < a.length; j++) {
                if (a[j] < temp) { //如果后一个数小于前一个数
                    temp = a[j];
                    position = j;
                }
            }
                     
            a[position] = a[i];
            a[i] = temp;
        }
                 
        //显示结果
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + ",");
        }
                 
    }
}


3.直接插入排序

(1)基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排

好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数

也是排好顺序的。如此反复循环,直到全部排好顺序。

(2)实例

冒泡排序、插入排序、选择排序的简单了解_插入排序_03

package com.hanchao.sort;
/***********************
 * 插入排序
 * @author:han   
 * @version:1.0       
 * @created:2013-10-14   
 ***********************
 */
public class InsertSort {
      
   public static void main(String[] args) {
      
      //    int[] a = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; 
        int[] a = {49,38,65,97,76,13,50}; 
        int temp=0;  //声明一个临时变量
        for(int i=1; i < a.length; i++){ 
           int j=i-1; 
           temp=a[i]; 
           for(; j>=0&&temp<a[j]; j--){ 
               a[j+1]=a[j];                       //将大于temp的值整体后移一个单位 
           } 
           a[j+1]=temp; 
        } 
        for(int i=0;i<a.length;i++) {
            System.out.print(a[i] + ","); 
        }
    }   
}
		/**
		 * 1.求从10到100中能被3或5整除的数的和
		 * 
		 */
		int sum = 0;
		for(int i = 10; i <= 100; i++) {
			if(i % 3 == 0 || i % 5 == 0) {
				sum += i;
			}
		}
		System.out.println(sum);
		
		/**
		 * 2.将一个字符串逆序,不要使用反转函数
		 */
		String message = "he saw a racecar";
		StringBuilder rev = new StringBuilder();
		for(int i = message.length() - 1; i >= 0;i--) {
			rev.append(message.charAt(i));
		}
		System.out.println(rev.toString());
		
		
		/**
		 * 3.反转一个栈【后进先出】
		 * pop():移除堆栈顶部的对象,并作为此函数的值返回该对象。 
		 * 		堆栈顶部的对象(Vector 对象中的最后一项)。 
		 * 
		 * peek():查看堆栈顶部的对象,但不从堆栈中移除它
		 * 			堆栈顶部的对象(Vector 对象的最后一项)。 
		 * 
		 */
		Stack<String> items = new Stack<String>();
		items.push("he"); //栈底
		items.push("saw");
		items.push("a");
		items.push("racecar"); //栈顶
		
		reverseStack(items);
		
		while(items.size()>0) {
			System.out.println("反转后的栈,从栈顶取:" + items.pop());
		}
		

			public static void reverseStack(Stack<String> stack) {
		Queue<String> rev = new LinkedList<String>();
		while(stack.size() > 0) {
			rev.offer(stack.pop());
		}
		while(rev.size() > 0) {
			stack.push(rev.poll());
			// poll()获取并移除此队列的头,如果此队列为空,则返回 null。
		}
	}		

这样也可以哦:

	public static void main(String[] args) {
		
		Stack<String> stack1 = new Stack<String>();
		stack1.push("he");
		stack1.push("saw");
		stack1.push("a");
		stack1.push("racecar");

		System.out.println("***********************");
		
		Stack<String> stack2 = new Stack<String>();
		while(stack1.size() > 0) {
			stack2.push(stack1.pop());
		}
		
		while(stack2.size() > 0) {
			System.out.println(stack2.pop());
		}
		
	}