@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
int[] b = {49, 38, 65, 97, 76, 23, 17};
// InsetSort(b);
// ShellInset(b);
// SelectSort(b);
// SelectSort2(b);
// BuildingHeap(b);
// HeapSort(b);
// bubbleSort(b);
// bubbleSort1(b);
// bubbleSort2(b);
// quickSort(b,0,b.length-1);
// quickSort1(b,0,b.length-1,3);
// System.out.println();
for (int i = 0;i<b.length;i++){
System.out.print(b[i]+" ");
}
}
//插入排序 时间复杂度O(n^2)
private void InsetSort(int[] a) {
int len = a.length;
for (int i = 1; i < len; i++) {
if (a[i - 1] > a[i]) {
int j = i - 1;
int x = a[i];
a[i] = a[i - 1];
while (j >= 0 && x < a[j]) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = x;
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
private void ShellInsertSort(int[] a, int d) {
int len = a.length;
for (int i = d; i < len; i++) {
if (a[i - d] > a[i]) {
int j = i - d;
int x = a[i];
a[i] = a[i - d];
while (j >= 0 && x < a[j]) {
a[j + d] = a[j];
j = j - d;
}
a[j + d] = x;
}
}
}
private void ShellInset(int[] a) {
int d = a.length / 2;
while (d >= 1) {
ShellInsertSort(a, d);
d = d / 2;
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " --> ");
}
}
//选择排序 时间复杂度O(n^2)
private void SelectSort(int[] a) {
int len = a.length;
for (int i = 0; i < len; i++) {
int k = i;
for (int j = i + 1; j < len; j++) {
if (a[k] > a[j]) {
k = j;
}
}
if (k != i) {
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ---> ");
}
}
// 选择排序 改进方法,二元排序
private void SelectSort2(int[] a){
int len = a.length;
int i, j, min, max ,tmp;
for (i = 0; i <= len/2; i ++) { // i 跑 n / 2 趟排序就会排序完成
min = i ;max = i; // 先将 min 和 max 都指向待排序的第一个元素
for (j = i+1; j < len - i; j++) {
if (a[j] < a[min]) {
min = j;
}
if (a[j] > a[max]) {
max = j;
continue;
}
}
int mintemp = a[min];
System.out.println();
a[min] = a[i];
a[i] = mintemp;
if (max ==i){
int maxtemp = a[len-i-1];
a[len-i-1] = a[min];
a[min] = maxtemp;
} else {
tmp = a[len-i-1];
a[len-i-1] = a[max];
a[max] = tmp;
}
}
for (int ii = 0; ii < a.length; ii++) {
System.out.print( " " +a[ii] + " ");
}
}
void HeapAdjust(int H[],int s, int length)
{
int tmp = H[s];
int child = 2*s+1; //左孩子结点的位置。(i+1 为当前调整结点的右孩子结点的位置)
while (child < length) {
if(child+1 <length && H[child]<H[child+1]) { // 如果右孩子大于左孩子(找到比当前待调整结点大的孩子结点)
++child ;
}
if(H[s]<H[child]) { // 如果较大的子结点大于父结点
H[s] = H[child]; // 那么把较大的子结点往上移动,替换它的父结点
s = child; // 重新设置s ,即待调整的下一个结点的位置
child = 2*s+1;
} else { // 如果当前待调整结点大于它的左右孩子,则不需要调整,直接退出
break;
}
H[s] = tmp; // 当前待调整的结点放到比其大的孩子结点位置上
}
}
/**
* 初始堆进行调整
* 将H[0..length-1]建成堆
* 调整完之后第一个元素是序列的最小的元素
*/
void BuildingHeap(int H[])
{
int length = H.length;
//最后一个有孩子的节点的位置 i= (length -1) / 2
for (int i = (length -1) / 2 ; i >= 0; --i) {
HeapAdjust(H,i,length);
}
for (int ii = 0; ii < H.length; ii++) {
System.out.print( " " +H[ii] + " ");
}
}
//时间复杂度也为:O(nlogn )
void HeapSort(int H[])
{
//初始堆
BuildingHeap(H);
int length = H.length;
//从最后一个元素开始对序列进行调整
for (int i = length - 1; i > 0; --i)
{
//交换堆顶元素H[0]和堆中最后一个元素
int temp = H[i]; H[i] = H[0]; H[0] = temp;
//每次交换堆顶元素和堆中最后一个元素之后,都要对堆进行调整
HeapAdjust(H,0,i);
}
System.out.println();
for (int ii = 0; ii < H.length; ii++) {
System.out.print( " " +H[ii] + " ");
}
}
void bubbleSort(int[] a){
int len = a.length;
for (int i = 0;i<len-1;i++){
for (int j = 0;j<len-i-1;j++){
if (a[j]>a[j+1]){
int tmp = a[j];
a[j] = a[j+1];
a[j+1]= tmp;
}
}
}
for (int ii = 0; ii < a.length; ii++) {
System.out.print( " " +a[ii] + " ");
}
}
void bubbleSort1(int[] a){
int i = a.length-1;
while (i>0){
int pos = 0;
for (int j =0;j<i;j++){
if (a[j]>a[j+1]){
pos = j;
int tmp = a[j];
a[j]= a[j+1];
a[j+1]= tmp;
}
}
i = pos;
}
for (int ii = 0; ii < a.length; ii++) {
System.out.print( " " +a[ii] + " ");
}
}
void bubbleSort2(int[] a){
int low =0;
int hight = a.length-1;
int j ,tmp;
while (low<hight){
for (j= low;j<hight;j++){
if (a[j]>a[j+1]){
tmp = a[j];
a[j]=a[j+1];
a[j+1]= tmp;
}
}
hight--;
for (j=hight;j>low;j--){
if (a[j]<a[j-1]){
tmp = a[j-1];
a[j-1]= a[j];
a[j]=tmp;
}
}
low++;
}
for (int ii = 0; ii < a.length; ii++) {
System.out.print( " " +a[ii] + " ");
}
}
//空间复杂度(O(nlog2n))
void quickSort(int[]a ,int left,int right){
if (left<right){
int l = left;
int r = right;
int tmp = a[l];
while (l<r){
while (l<r && a[r]>=tmp){
r--;
}
a[l]=a[r];
while (l<r&&a[l]<=tmp){
l++;
}
a[r]=a[l];
}
a[l]=tmp;
quickSort(a,left,l-1);
quickSort(a,l+1,right);
}
}
void quickSort1(int[] a ,int left,int right,int k){
if (right-left>k){
int l = left;
int r = right;
int tmp = a[l];
while (l<r){
while (l<r&&tmp<=a[r]){
r--;
}
a[l]=a[r];
while (l<r&&tmp>=a[l]){
l++;
}
a[r]= a[l];
}
a[l]=tmp;
quickSort1(a,left,l-1,k);
quickSort1(a,l+1,right,k);
}
for (int i = 1;i<a.length;i++){
if (a[i-1]>a[i]){
int j = i-1;
int temp = a[i];
while (j>=0&&a[j]>temp){
a[j+1]=a[j];
j--;
}
a[j+1]= temp;
}
}
}
基本排序算法
最新推荐文章于 2021-06-08 01:10:18 发布