---
title: "排序算法-冒泡排序"
author: "zenoh"
date: "2011-09-25T18:50:44Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-09-25T18:50:44Z"
id: 1180119
url: "https://zenoh.iteye.com/blog/1180119"
---

冒泡排序

这可能是最简单的排序算法了，算法思想是每次从数组末端开始比较相邻两元素，把第i小的冒泡到数组的第i个位置。i从0一直到N-1从而完成排序。（当然也可以从数组开始端开始比较相邻两元素，把第i大的冒泡到数组的第N-i个位置。i从0一直到N-1从而完成排序

 

```
public class BubbleSorter<T extends Comparable<T>> extends Sorter<T>{

	public static void bubbleSort(int array){
		
	}

	@Override
	public void sort(T[] array, int from, int len) {
		for (int i= from ; i<from+len ; i++){
			for(int j=i+1 ; j<from+len;j++){
				if(array[i].compareTo(array[j])>0){
					swap(array, i, j) ;
				}
			}
		}
	}
	
	public static void main(String[] args) {
		Integer array[] = {2,35,5,78,1,90,13,56} ;
		for (int i = 0 ; i<array.length ; i++){
			System.out.print(array[i]+",");
		}
		System.out.println();
		Sorter sorter = new BubbleSorter<Integer>() ;
		sorter.sort(array) ;
			
		for (int i = 0 ; i<array.length ; i++){
			System.out.print(array[i]+",");
		}				
		}
}
```

 简单不解释