为什么我打印出的数组没有在下面的代码中排序?
public class BubbleSort {
public void sortArray(int[] x) {//go through the array and sort from smallest to highest
for(int i=1; i<x.length; i++) {
int temp=0;
if(x[i-1] > x[i]) {
temp = x[i-1];
x[i-1] = x[i];
x[i] = temp;
}
}
}
public void printArray(int[] x) {
for(int i=0; i<x.length; i++)
System.out.print(x[i] + " ");
}
public static void main(String[] args) {
// TestBubbleSort
BubbleSort b = new BubbleSort();
int[] num = {5,4,3,2,1};
b.sortArray(num);
b.printArray(num);
}
}
答案 0 :(得分:46)
您需要两个循环来实现冒泡排序。
示例代码:
public static void bubbleSort(int[] numArray) {
int n = numArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (numArray[j - 1] > numArray[j]) {
temp = numArray[j - 1];
numArray[j - 1] = numArray[j];
numArray[j] = temp;
}
}
}
}
答案 1 :(得分:12)
您只需通过阵列一次!冒泡排序要求你保持循环直到你发现你不再进行任何交换;因此O(n ^ 2)的运行时间。
试试这个:
public void sortArray(int[] x) {
boolean swapped = true;
while (swapped) {
swapped = false;
for(int i=1; i<x.length; i++) {
int temp=0;
if(x[i-1] > x[i]) {
temp = x[i-1];
x[i-1] = x[i];
x[i] = temp;
swapped = true;
}
}
}
}
在循环结束时swapped == false
一次,您已经完成了一次传递而没有找到x[i-1] > x[i]
的任何实例,因此您知道数组已排序。只有然后才能终止算法。
您还可以使用while
次迭代的for循环替换外部n+1
循环,这将保证数组按顺序排列;但是,while
循环具有在优于最差情况下提前终止的优势。
答案 2 :(得分:2)
你的排序逻辑错了。这是冒泡排序的伪代码:
for i = 1:n,
swapped = false
for j = n:i+1,
if a[j] < a[j-1],
swap a[j,j-1]
swapped = true
→ invariant: a[1..i] in final position
break if not swapped
end
有关所有各种排序方法的详细教程,请参阅此sorting web site。
答案 3 :(得分:1)
我使用此方法进行冒泡分类
i
答案 4 :(得分:0)
这不是冒泡排序算法,您需要重复,直到您无需交换:
public void sortArray(int[] x) {//go through the array and sort from smallest to highest
for(;;) {
boolean s = false;
for(int i=1; i<x.length; i++) {
int temp=0;
if(x[i-1] > x[i]) {
temp = x[i-1];
x[i-1] = x[i];
x[i] = temp;
s = true;
}
}
if (!s) return;
}
}
答案 5 :(得分:0)
冒泡排序嵌套循环应该这样写:
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(intArray[j-1] > intArray[j]){
//swap the elements!
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
答案 6 :(得分:0)
public static void BubbleSort(int[] array){
boolean swapped ;
do {
swapped = false;
for (int i = 0; i < array.length - 1; i++) {
if (array[i] > array[i + 1]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
}while (swapped);
}
答案 7 :(得分:0)
public class Bubblesort{
public static int arr[];
public static void main(String args[]){
System.out.println("Enter number of element you have in array for performing bubblesort");
int numbofele = Integer.parseInt(args[0]);
System.out.println("numer of element entered is"+ "\n" + numbofele);
arr= new int[numbofele];
System.out.println("Enter Elements of array");
System.out.println("The given array is");
for(int i=0,j=1;i<numbofele;i++,j++){
arr[i]=Integer.parseInt(args[j]);
System.out.println(arr[i]);
}
boolean swapped = false;
System.out.println("The sorted array is");
for(int k=0;k<numbofele-1;k++){
for(int l=0;l+1<numbofele-k;l++){
if(arr[l]>arr[l+1]){
int temp = arr[l];
arr[l]= arr[l+1];
arr[l+1]=temp;
swapped=true;
}
}
if(!swapped){
for(int m=0;m<numbofele;m++){
System.out.println(arr[m]);
}
return;
}
}
for(int m=0;m<numbofele;m++){
System.out.println(arr[m]);
}
}
}
答案 8 :(得分:0)
冒泡排序算法是对数组元素进行排序的最简单方法。大多数其他算法比冒泡排序算法更有效。最坏情况和平均情况下的时间复杂度为(n ^ 2)。让我们考虑如何实现冒泡排序算法
$employees = Employe::find()->with(['department.organization', 'organization'])->all();
foreach ($employees as $employee) {
echo $employee->getOrganizationModel()->name;
}
}
答案 9 :(得分:0)
我们在这里
static String arrayToString(int[] array) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < array.length; i++) {
stringBuilder.append(array[i]).append(",");
}
return stringBuilder.deleteCharAt(stringBuilder.length()-1).toString();
}
public static void main(String... args){
int[] unsorted = {9,2,1,4,0};
System.out.println("Sorting an array of Length "+unsorted.length);
enhancedBubbleSort(unsorted);
//dumbBubbleSort(unsorted);
//bubbleSort(unsorted);
//enhancedBubbleSort(unsorted);
//enhancedBubbleSortBetterStructured(unsorted);
System.out.println("Sorted Array: "+arrayToString(unsorted));
}
// this is the dumbest BubbleSort
static int[] dumbBubbleSort(int[] array){
for (int i = 0; i<array.length-1 ; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
// Just swap
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
System.out.println("After "+(i+1)+" pass: "+arrayToString(array));
}
return array;
}
//this "-i" in array.length - 1-i brings some improvement.
// Then for making our bestcase scenario better ( o(n) , we will introduce isswapped flag) that's enhanced bubble sort
static int[] bubbleSort(int[] array){
for (int i = 0; i<array.length-1 ; i++) {
for (int j = 0; j < array.length - 1-i; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
System.out.println("After "+(i+1)+" pass: "+arrayToString(array));
}
return array;
}
static int[] enhancedBubbleSort(int[] array){
int i=0;
while (true) {
boolean swapped = false;
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped =true;
}
}
i++;
System.out.println("After "+(i)+" pass: "+arrayToString(array));
if(!swapped){
// Last iteration (of outer loop) didnot result in any swaps. so stopping here
break;
}
}
return array;
}
static int[] enhancedBubbleSortBetterStructured(int[] array){
int i=0;
boolean swapped = true;
while (swapped) {
swapped = false;
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
i++;
System.out.println("After "+(i)+" pass: "+arrayToString(array));
}
return array;
}
答案 10 :(得分:0)
这里是 bubbleSort 的示例,时间复杂度为O(n)。
@if($silver->count() > 0)
something
@endif
答案 11 :(得分:0)
确实没有得到bubble sort
,可以更恰当地称其为“沉没式”,因为它实际上将更大/更重的沉入底部,这是给出的大多数答案。最典型的一种是
private static void sortZero(Comparable[] arr) {
boolean moreSinkingRequired = true;
while (moreSinkingRequired) {
moreSinkingRequired = false;
for (int j = 0; j < arr.length - 1; ++j) {
if (less(arr, j + 1, j)) {
exch(arr, j, j + 1);
moreSinkingRequired = true;
}
}
}
}
顺便说一句,您必须比实际停下来再走一遍。
但是我看到它像在冒泡
private static void sortOne(Comparable[] arr) {
for (int i = 0; i < arr.length - 1; ++i) {
for (int j = arr.length - 1; j > i; --j) { // start from the bottom
if (less(arr, j, j - 1)) {
exch(arr, j - 1, j);
}
}
}
}
您必须知道这种方法实际上更好,因为它跟踪端点以供j > i
与已经排序的[0, i-1]
进行比较。
我们还可以添加更早的终止,但随后会变得冗长
private static void sortTwo(Comparable[] arr) {
boolean moreBubbleRequired = true;
for (int i = 0; i < arr.length - 1 && moreBubbleRequired; ++i) {
moreBubbleRequired = false;
for (int j = arr.length - 1; j > i; --j) {
if (less(arr, j, j - 1)) {
exch(arr, j - 1, j);
moreBubbleRequired = true;
}
}
}
}
用于简化流程的utils
public static boolean less(Comparable[] arr, int i, int j) {
return less(arr[i], arr[j]);
}
public static boolean less(Comparable a, Comparable b) {
return a.compareTo(b) < 0;
}
public static void exch(Comparable[] arr, int i, int j) {
Comparable t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
答案 12 :(得分:-1)
public class SortingArray {
public static void main(String[] args) {
int[] a={3,7,9,5,1,4,0,2,8,6};
int temp=0;
boolean isSwapped=true;
System.out.println(" before sorting the array: ");
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);
}
System.out.println("");
do
{
isSwapped=false;
for(int i=0;i<a.length-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}while(isSwapped);
System.out.println("after sorting the array: ");
for(int array:a)
{
System.out.print(array);
}
}
}
答案 13 :(得分:-1)
public class BubbleSort {
public void sorter(int[] arr, int x, int y){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public void sorter1(String[] arr, int x, int y){
String temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public void sertedArr(int[] a, String[] b){
for(int j = 0; j < a.length - 1; j++){
for(int i = 0; i < a.length - 1; i++){
if(a[i] > a[i + 1]){
sorter(a, i, i + 1);
sorter1(b, i, i + 1);
}
}
}
for(int i = 0; i < a.length; i++){
System.out.print(a[i]);
}
System.out.println();
for(int i = 0; i < b.length; i++){
System.out.print(b[i]);
}
//
}
public static void main(String[] args){
int[] array = {3, 7, 4, 9, 5, 6};
String[] name = {"t", "a", "b", "m", "2", "3"};
BubbleSort bso = new BubbleSort();
bso.sertedArr(array, name);
}
}
答案 14 :(得分:-1)
java代码
public void bubbleSort(int[] arr){
boolean isSwapped = true;
for(int i = arr.length - 1; isSwapped; i--){
isSwapped = false;
for(int j = 0; j < i; j++){
if(arr[j] > arr[j+1]}{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
isSwapped = true;
}
}
}
}
答案 15 :(得分:-2)
Java中的标准冒泡排序实现:
//Time complexity: O(n^2)
public static int[] bubbleSort(int[] arr) {
if (arr == null || arr.length <= 1) {
return arr;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
arr[j] = arr[j] + arr[j - 1];
arr[j - 1] = arr[j] - arr[j - 1];
arr[j] = arr[j] - arr[j - 1];
}
}
}
return arr;
}