用Java语言实现的各种排序,包括插入排序、冒泡排序、选择排序、Shell排序、快速排序、归并排序、堆排序、SortUtil等。
- 插入排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class InsertSort implements SortUtil.Sort
- {
-
-
-
- public void sort(int[] data)
- {
- int temp;
- for(int i=1;i for(int j=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1);
- }
- }
-
- 冒泡排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class BubbleSort implements SortUtil.Sort
- {
-
-
-
- public void sort(int[] data)
- {
- int temp;
- for(int i=0;i for(int j=data.length-1;j>i;j--)
- {
- if(data[j] SortUtil.swap(data,j,j-1);
- }
- }
- }
-
- 选择排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class SelectionSort implements SortUtil.Sort
- {
-
-
-
-
-
- public void sort(int[] data)
- {
- int temp;
- for (int i = 0; i < data.length; i++)
- {
- int lowIndex = i;
- for (int j = data.length - 1; j >i; j--)
- {
- if (data[j] < data[lowIndex])
- {
- lowIndex = j;
- }
- }
- SortUtil.swap(data,i,lowIndex);
- }
- }
- }
- Shell排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class ShellSort implements SortUtil.Sort
- {
-
-
-
- public void sort(int[] data)
- {
- for(int i=data.length/2;i>2;i/=2)
- {
- for(int j=0;j insertSort(data,j,i);
- }
- }
- insertSort(data,0,1);
- }
-
-
-
-
-
-
- private void insertSort(int[] data, int start, int inc)
- {
- int temp;
- for(int i=start+inc;i for(int j=i;(j>=inc)&&(data[j] SortUtil.swap(data,j,j-inc);
- }
-
- 快速排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class QuickSort implements SortUtil.Sort
- {
-
-
-
- public void sort(int[] data)
- {
- quickSort(data,0,data.length-1);
- }
- private void quickSort(int[] data,int i,int j)
- {
- int pivotIndex=(i+j)/2;
-
- SortUtil.swap(data,pivotIndex,j);
- int k=partition(data,i-1,j,data[j]);
- SortUtil.swap(data,k,j);
- if((k-i)>1) quickSort(data,i,k-1);
- if((j-k)>1) quickSort(data,k+1,j);
- }
-
-
-
-
-
-
- private int partition(int[] data, int l, int r,int pivot)
- {
- do
- {
- while(data[++l] while((r!=0)&&data[--r]>pivot);
- SortUtil.swap(data,l,r);
- }
- while(l SortUtil.swap(data,l,r);
- return l;
- }
- }
-
- 改进后的快速排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class ImprovedQuickSort implements SortUtil.Sort
- {
- private static int MAX_STACK_SIZE=4096;
- private static int THRESHOLD=10;
-
-
-
- public void sort(int[] data)
- {
- int[] stack=new int[MAX_STACK_SIZE];
- int top=-1;
- int pivot;
- int pivotIndex,l,r;
- stack[++top]=0;
- stack[++top]=data.length-1;
- while(top>0)
- {
- int j=stack[top--];
- int i=stack[top--];
- pivotIndex=(i+j)/2;
- pivot=data[pivotIndex];
- SortUtil.swap(data,pivotIndex,j);
-
- l=i-1;
- r=j;
- do
- {
- while(data[++l] while((r!=0)&&(data[--r]>pivot));
- SortUtil.swap(data,l,r);
- }
- while(l SortUtil.swap(data,l,r);
- SortUtil.swap(data,l,j);
- if((l-i)>THRESHOLD)
- {
- stack[++top]=i;
- stack[++top]=l-1;
- }
- if((j-l)>THRESHOLD)
- {
- stack[++top]=l+1;
- stack[++top]=j;
- }
- }
-
- insertSort(data);
- }
-
-
-
- private void insertSort(int[] data)
- {
- int temp;
- for(int i=1;i for(int j=i;(j>0)&&(data[j] SortUtil.swap(data,j,j-1);
- }
- }
- 归并排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class MergeSort implements SortUtil.Sort
- {
-
-
-
- public void sort(int[] data)
- {
- int[] temp=new int[data.length];
- mergeSort(data,temp,0,data.length-1);
- }
- private void mergeSort(int[] data,int[] temp,int l,int r)
- {
- int mid=(l+r)/2;
- if(l==r) return ;
- mergeSort(data,temp,l,mid);
- mergeSort(data,temp,mid+1,r);
- for(int i=l;i<=r;i++){
- temp[i]=data[i];
- }
- int i1=l;
- int i2=mid+1;
- for(int cur=l;cur<=r;cur++)
- {
- if(i1==mid+1)
- data[cur]=temp[i2++];
- else if(i2>r)
- data[cur]=temp[i1++];
- else if(temp[i1] data[cur]=temp[i1++];
- else
- data[cur]=temp[i2++];
- }
- }
-
- 改进后的归并排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class ImprovedMergeSort implements SortUtil.Sort
- {
- private static final int THRESHOLD = 10;
-
-
-
-
-
- public void sort(int[] data)
- {
- int[] temp=new int[data.length];
- mergeSort(data,temp,0,data.length-1);
- }
- private void mergeSort(int[] data, int[] temp, int l, int r)
- {
- int i, j, k;
- int mid = (l + r) / 2;
- if (l == r)
- return;
- if ((mid - l) >= THRESHOLD)
- mergeSort(data, temp, l, mid);
- else
- insertSort(data, l, mid - l + 1);
- if ((r - mid) >THRESHOLD)
- mergeSort(data, temp, mid + 1, r);
- else
- insertSort(data, mid + 1, r - mid);
- for (i = l; i <= mid; i++)
- {
- temp[i] = data[i];
- }
- for (j = 1; j <= r - mid; j++)
- {
- temp[r - j + 1] = data[j + mid];
- }
- int a = temp[l];
- int b = temp[r];
- for (i = l, j = r, k = l; k <= r; k++)
- {
- if (a < b)
- {
- data[k] = temp[i++];
- a = temp[i];
- }
- else
- {
- data[k] = temp[j--];
- b = temp[j];
- }
- }
- }
-
-
-
-
-
-
- private void insertSort(int[] data, int start, int len)
- {
- for(int i=start+1;i for(int j=i;(j>start) && data[j] SortUtil.swap(data,j,j-1);
- }
- }
- 堆排序:
-
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
-
-
-
-
-
- public class HeapSort implements SortUtil.Sort
- {
-
-
-
- public void sort(int[] data)
- {
- MaxHeap h=new MaxHeap();
- h.init(data);
- for(int i=0;i h.remove();
- System.arraycopy(h.queue,1,data,0,data.length);
- }
- private static class MaxHeap
- {
- void init(int[] data)
- {
- this.queue=new int[data.length+1];
- for(int i=0;i queue[++size]=data[i];
- fixUp(size);
- }
- }
- private int size=0;
- private int[] queue;
- public int get()
- {
- return queue[1];
- }
- public void remove()
- {
- SortUtil.swap(queue,1,size--);
- fixDown(1);
- }
-
- private void fixDown(int k)
- {
- int j;
- while ((j = k << 1) <= size)
- {
- if (j < size && queue[j] j++;
- if (queue[k]>queue[j])
- break;
- SortUtil.swap(queue,j,k);
- k = j;
- }
- }
- private void fixUp(int k)
- {
- while (k >1)
- {
- int j = k >>1;
- if (queue[j]>queue[k])
- break;
- SortUtil.swap(queue,j,k);
- k = j;
- }
- }
- }
-
- SortUtil:
-
- package org.rut.util.algorithm;
- import org.rut.util.algorithm.support.BubbleSort;
- import org.rut.util.algorithm.support.HeapSort;
- import org.rut.util.algorithm.support.ImprovedMergeSort;
- import org.rut.util.algorithm.support.ImprovedQuickSort;
- import org.rut.util.algorithm.support.InsertSort;
- import org.rut.util.algorithm.support.MergeSort;
- import org.rut.util.algorithm.support.QuickSort;
- import org.rut.util.algorithm.support.SelectionSort;
- import org.rut.util.algorithm.support.ShellSort;
-
-
-
-
-
-
- public class SortUtil
- {
- public final static int INSERT = 1;
- public final static int BUBBLE = 2;
- public final static int SELECTION = 3;
- public final static int SHELL = 4;
- public final static int QUICK = 5;
- public final static int IMPROVED_QUICK = 6;
- public final static int MERGE = 7;
- public final static int IMPROVED_MERGE = 8;
- public final static int HEAP = 9;
- public static void sort(int[] data)
- {
- sort(data, IMPROVED_QUICK);
- }
- private static String[] name=
- {
- "insert", "bubble", "selection", "shell", "quick", "improved_quick", "merge", "improved_merge", "heap"
- };
- private static Sort[] impl=new Sort[]
- {
- new InsertSort(),
- new BubbleSort(),
- new SelectionSort(),
- new ShellSort(),
- new QuickSort(),
- new ImprovedQuickSort(),
- new MergeSort(),
- new ImprovedMergeSort(),
- new HeapSort()
- };
- public static String toString(int algorithm)
- {
- return name[algorithm-1];
- }
- public static void sort(int[] data, int algorithm)
- {
- impl[algorithm-1].sort(data);
- }
- public static interface Sort
- {
- public void sort(int[] data);
- }
- public static void swap(int[] data, int i, int j)
- {
- int temp = data[i];
- data[i] = data[j];
- data[j] = temp;
- }
- }
分享到:
相关推荐
根据提供的文件信息,我们可以总结出该文档主要涉及了五种基于...以上就是关于这五种排序算法的介绍及其基于Java的实现方式。这些排序算法各有优缺点,适用于不同的场景,了解并掌握它们能够帮助开发者更好地解决问题。
### Java 实现数据结构常见排序算法及详解 #### 排序算法概述 排序算法是计算机科学中的基础概念之一,主要用于将一系列数据按照特定规则进行排列。根据数据处理方式的不同,排序算法大致分为两大类:比较排序与非...
本文将详细探讨标题所提及的几种排序算法:合并排序、插入排序、希尔排序、快速排序、冒泡排序以及桶排序,并结合Java语言的实现进行解析。 1. **合并排序(Merge Sort)**: 合并排序是一种基于分治策略的排序算法...
Java几种常见的排序算法
7. **计数排序(Counting Sort)、桶排序(Bucket Sort)和基数排序(Radix Sort)**:这三种排序算法属于非比较型排序,不依赖于元素间的比较,而是基于特定的特性,如元素的范围、分布等。Java中实现这类排序通常...
本文将详细探讨Java中实现几种常见的排序算法,包括它们的工作原理、时间复杂度以及如何在实际代码中应用。 一、冒泡排序(Bubble Sort) 冒泡排序是一种简单的排序算法,它重复地遍历待排序的数列,一次比较两个...
### Java 实现几种常见排序方法 #### 泡泡排序(Bubble Sort) ...以上是几种常见的排序算法在 Java 中的具体实现,每种算法都有其特点和适用场景。在实际应用中可以根据具体需求选择最合适的排序算法。
本文将深入探讨在Java中实现的几种常见排序算法:冒泡排序、快速排序以及堆排序。 1. **冒泡排序(Bubble Sort)** 冒泡排序是最简单的排序算法之一,通过重复遍历数组,比较相邻元素并交换位置,直到没有任何一对...
JAVA几种常用的经典的排序算法 冒泡 选择 快速 shell 堆排序
几种常见排序算法JAVA实现.pdf
本文将深入探讨Java中常见的几种基本排序算法,包括插入排序、交换排序、选择排序以及归并排序,并分析它们的基本思想、时间复杂度以及应用场景。 #### 插入排序 插入排序是一种简单的排序方法,它的工作原理类似...
根据给定的文件信息,我们可以深入探讨几种在Java中实现的经典排序算法,这些算法是数据结构与算法领域的重要组成部分,广泛应用于各种计算机科学场景。以下是对插入排序(Insertion Sort)、冒泡排序(Bubble Sort...
### Java几种常用的排序算法 在Java编程语言中,排序算法是数据处理中不可或缺的一部分,它不仅可以帮助我们组织数据,还能提高程序效率。本篇文章将详细介绍几种常用的Java排序算法:选择排序、冒泡排序以及插入...