一、冒泡排序package SortingAlgorithm;
public class BubbleSort
{
public static void main(String[] args)
{
int[] array = {6,3,4,0,7,8,5,9,2,1};
BubbleSort bubblesort = new BubbleSort();
bubblesort.bubbleSort(array);
bubblesort.showArray(array);
}
public static void bubbleSort(int[] arr)
{
int n = arr.length;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
// 交換 arr[j] 和 arr[j + 1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public void showArray(int[] arr)
{
for(int i:arr)
{
System.out.print(i+" ");
}
}
} def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(n-i-1):
if arr[j]>arr[j+1]:
arr[j],arr[j+1] = arr[j+1],arr[j]
array=[6,3,4,0,7,8,5,9,2,1]
bubble_sort(array)
for i in range(len(array)):
print(array[i]) 二、選擇排序public class SelectionSort
{
public static void main(String[] args)
{
int[] array={45,32,14,54,23};
SelectionSort selectionsort = new SelectionSort();
selectionsort.selectionSort(array);
selectionsort.showArray(array);
}
public static void selectionSort(int[] arr)
{
int n = arr.length;
for(int i=0;i<n-1;i++)
{
int min_idx=i;
for(int j=i+1;j<n;j++)
{
if (arr[j]<arr[min_idx])
{
min_idx=j;
}
}
int temp = arr[min_idx];
arr[min_idx]=arr[i];
arr[i]=temp;
}
}
public void showArray(int[] arr)
{
for(int i:arr)
{
System.out.print(i+" ");
}
}
} def selection_sort(arr):
n = len(arr)
for i in range(n-1):
min_idx = i
for j in range(i+1,n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i],arr[min_idx] = arr[min_idx],arr[i]
array=[45,32,14,54,23]
selection_sort(array)
for i in range(len(array)):
print(array[i]) 三、插入排序public class InsertionSort
{
public static void main(String[] args)
{
int[] array = {42,20,17,13,28,14,23,15};
InsertionSort insertionsort = new InsertionSort();
insertionsort.insertionSort(array);
insertionsort.showArray(array);
}
public static void insertionSort(int[] arr)
{
int n=arr.length;
for(int i=1;i<n;i++)
{
int key = arr[i];
int j=i-1;
while(j>=0 && arr[j]>key)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
}
public static void showArray(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
} def insertion_sort(arr):
n = len(arr)
for i in range(1,n):
key = arr[i]
j = i - 1
while j>= 0 and arr[j] > key:
arr[j+1] = arr[j]
j-=1
arr[j+1] = key
array = [42,20,17,13,28,14,23,15]
insertion_sort(array)
for i in range(len(array)):
print(array[i]) 四、快速排序public class QuickSort
{
public static void main(String[] args)
{
int[] array={4,7,6,5,3,2,8,1};
int n = array.length;
QuickSort quicksort = new QuickSort();
quicksort.quickSort(array,0,n-1);
quicksort.showArray(array);
}
public static void quickSort(int[] arr,int low,int high)
{
if(low<high)
{
int pivotIndex = partition(arr,low,high);
quickSort(arr,low,pivotIndex-1);
quickSort(arr,pivotIndex+1,high);
}
}
public static int partition(int[] arr,int low,int high)
{
int pivot = arr[high];
int i=low-1;
for(int j=low;j<high;j++)
{
if (arr[j] <= pivot)
{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1]=arr[high];
arr[high]=temp;
return i+1;
}
public static void showArray(int[] arr)
{
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
} def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
array = [4,7,6,5,3,2,8,1]
sorted_arr=quick_sort(array)
for i in range(len(sorted_arr)):
print(sorted_arr[i])
|