到目前为止,这是我的代码.我在第一个for语句中得到了一个不兼容的类型.必需:boolean Found:int
不太清楚我需要做些什么来解决这个问题.
public void selectionSort(int[] list){
//implement selection sort here.
for (int i = 0;list.length -1;i++){
int smvi = i;
for(int j = i+1;j<list.length;j++){
if(list[j] < list[smvi]){
smvi=j;}
if(i!=smvi){
int temp = list[i];
list[i] = list[smvi];
list[smvi] = temp;
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 新程序员来了!我正在实现选择排序,使用一个最大变量而不是通常的最小值,但我仍然想从最低到最高排序。下面的代码对列表进行了完美的排序,除了第一个值是一个非常大的负数......我可以修复什么以使其正确排序的任何提示?
void selection(int Array[], int size) {
int i, j, max, temp;
for (i = 0; i < size-1; i++) {
max = i;
for (j = i+1; j <= size; j++) {
if ( Array[j] < Array[max] )
max = j;
}
temp = Array[max];
Array[max] = Array[i];
Array[i] = temp;
}
}
Run Code Online (Sandbox Code Playgroud) 如果数组已排序,那么如何停止排序.通过选择排序对排序数组进行排序.时间复杂度为0(1).
static void sort(int a[]){
int min;
for(int i=0;i<a.length-1;i++){
min=i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[min])
min=j;
}
if(min==0){
System.out.print("min" + min);
break;
}
int temp=a[min];
a[min]=a[i];
a[i]=temp;
}
System.out.print(Arrays.toString(a) );
}
Run Code Online (Sandbox Code Playgroud) 我正在研究一个比较时间气泡和选择排序的项目。我制作了两个单独的程序并将它们合并为一个,现在冒泡排序的运行速度比选择排序快得多。我进行了检查,以确保代码不仅仅因为一些转换错误而给我 0,而且正在按预期运行。我用它System.Diagnostics;来测量时间。我还检查了机器没有问题,我在Replit上运行它并得到了类似的结果。
{
class Program
{
public static int s1 = 0;
public static int s2 = 0;
static decimal bubblesort(int[] arr1)
{
int n = arr1.Length;
var sw1 = Stopwatch.StartNew();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr1[j] > arr1[j + 1])
{
int tmp = arr1[j];
// swap tmp and arr[i] int tmp = arr[j];
arr1[j] …Run Code Online (Sandbox Code Playgroud)