源文件
public class Sorter
{
public void bubbleSort(int[] a)
{
for(int i=0;i<a.length-1;i++)
{
for(int j=0;j<a.length-1;j++)
{
change(a,j,j+1);
}
}
print(a);
}
public void selectSort(int[] a)
{
for(int i=0;i<a.length-1;i++)
{
int temp = i;
for(int j=i+1;j<a.length;j++)
{
if(a[temp]>a[j])
{
temp=j;
}
}
change(a,i,temp);
}
print(a);
}
public void insertSort(int[] a)
{
for(int i=1;i<a.length;i++)
{
int temp = a[i];
int j=i-1;
while(j>=0&&temp<a[j])
{
a[j+1] = a[j];
j--;
}
a[j+1]=temp;
}
print(a);
}
private void change(int[] b,int i, int j)
{
int temp=0;
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
private void print(int[] a) {
for(int i=0;i<a.length-1;i++)
{
System.out.print(a[i]+"<");
}
System.out.println(a[a.length-1]);
}
}
//测试
public class Test
{
/**
* @param args
*/
public static void main(String[] args)
{
int[] a = new int[]{84,55,34,11,9,8,6,5,7,1};
Sorter sorter = new Sorter();
//sorter.bubbleSort(a);
//sorter.selectSort(a);
sorter.insertSort(a);
}
}
分享到:
相关推荐
这些排序算法各有优缺点,如冒泡排序和插入排序简单但效率较低,适用于小规模数据;选择排序和希尔排序在特定情况下有较好的性能;归并排序和快速排序则在大多数情况下都能提供较高的效率,尤其是快速排序,是实际...
根据给定文件中的标题、描述、标签以及部分内容,本文将详细介绍冒泡排序、选择排序、插入排序、希尔排序以及利用Java内置方法进行排序这五种排序算法的具体实现过程及其背后的逻辑原理。 ### 一、冒泡排序 冒泡...
下面是一个简单的Java冒泡排序实现: ```java public class BubbleSort { public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i ; i++) { // 外层循环控制遍历次数 for (int...
例如,冒泡排序和插入排序简单但效率较低,适合小规模数据;堆排序和快速排序在中大规模数据上表现良好,但快速排序的不稳定性和堆排序的空间复杂度是需要注意的问题;归并排序和希尔排序在稳定性上有优势,而桶排序...
在`Insert.java`文件中,插入排序的实现可能包括: - 初始化已排序部分为第一个元素,然后逐个将后续元素与已排序部分的元素比较,并在合适位置插入。 - 每次插入一个新元素,都会导致已排序部分增加一个元素。 ...
例如,插入排序和选择排序适合小规模数据,冒泡排序虽然效率较低但实现简单,堆排序和快速排序在处理大规模数据时有较好性能,而基数排序则能处理非负整数排序。在实际开发中,根据具体需求选择合适的排序算法是非常...
在本文中,我们将深入探讨Java编程中的三种基本排序算法:冒泡排序、插入排序和堆排序。这些排序算法是计算机科学中的基础知识,尤其对于初学者来说,理解和实现它们至关重要。我们将详细讲解每种排序算法的工作原理...
以上就是Java中快速排序、冒泡排序和插入排序的实现方式。快速排序的平均时间复杂度为O(n log n),冒泡排序和插入排序的平均时间复杂度为O(n^2)。在实际应用中,快速排序通常优于其他两种排序,尤其是在大数据量时。...
虽然冒泡排序不一定要进行 n-1 趟,但由于它的记录移动次数较多,故平均时间性能比直接插入排序要差得多。 (4)算法稳定性:稳定 冒泡排序是就地排序,且它是稳定的。 5. 算法改进 冒泡排序还可以做如下的改进...
Java中常见的三大排序算法包括插入排序、冒泡排序和选择排序。这三种排序算法各有特点,适用于不同的场景,理解并掌握它们对于提升编程技能非常有帮助。 **1. 插入排序** 插入排序是一种简单直观的排序算法,它的...
本文将详细介绍三种经典的排序算法:选择排序、冒泡排序和插入排序,并结合Java代码进行分析。 ### 1. 选择排序(Selection Sort) 选择排序的主要思想是每次从待排序的数据元素中选出最小(或最大)的一个元素,...
本文将深入探讨Java编程语言中实现的七种主要排序算法:直接插入排序、希尔排序、选择排序、堆排序、冒泡排序、快速排序以及归并排序。每种算法都有其独特性,适用于不同的场景和数据特性。 1. **直接插入排序**:...
java实现排序,各种排序算法,冒泡排序,选择排序,插入排序等等
JAVA实现选择,冒泡,归并,插入,快速排序。并随机生成不同规模的随机数来测试各种排序方法耗费的时间。
排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序.mht
JAVA排序大全 冒泡 快速 选择 归并排序
以下是根据标题和描述中提到的四种排序算法——冒泡排序、快速排序、插入排序和选择排序的详细说明。 **冒泡排序(BuddleSort)**: 冒泡排序是一种简单的交换排序,它通过重复遍历待排序的列表,比较相邻元素并...
数据结构之排序算法,使用Java实现,包括冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序_Data-Structure-Sort-Algorithm
本文将详细讨论两种常见的简单排序算法:冒泡排序和直接插入排序,它们都是基于比较的内部排序算法,并且在Java语言中实现。 **冒泡排序(Bubble Sort)** 冒泡排序是一种基础的排序算法,其基本思想是通过重复...
总的来说,这段代码提供了四种排序算法的实现,分别是冒泡排序、选择排序、插入排序以及Java内置的数组排序。每种排序算法都有其适用场景,理解这些算法可以帮助我们更好地解决实际问题,并根据需求选择合适的排序...