我试图在python中实现一个SelectionSort算法,因此我创建了一个numpy数组,并希望将它作为参数传递到算法中。
def SelectionSort(array=None):
for i in range(len(array)):
for j in range(i+1,len(array)):
if(array[j]<array[i]):
array[i],array[j]=array[j],array[I]
但我得到了这个ValueError:
-------------------
我的问题是,selectionsort显示了错误的交换数量。它总是显示0或某个较大的数字。当给定的数组被排序时,它总是显示一个很大的数字,或者所有其他未排序的测试总是0。
//this class is called selectionSort. It sorts a given array.
public class SelectionSort implements ISorter {
private int swaps;
public SelectionSort() {}
@Override
public ISortStats sort(int[] a
我一直在研究选择排序的递归版本。MaxInd在SelectionSort的一次迭代中工作得很好,但是当我进行SelectionSort递归之后,MaxInd在SelectionSort的第一次迭代之后开始产生不正确的值,这导致我的代码交换了不正确的值。我不知道它为什么要这么做。
#include <stdio.h>
int MaxInd(int arr[], int i, int len, int max, int index){
if (arr[i]>max){
max=arr[i];
index=i;
}
if(i
我是一个初学者,现在我正试着在第二天实现SelectionSort以达到练习的目的。我的算法在大多数情况下都有效,但并不总是有效。不幸的是,我完全不清楚为什么它并不总是有效。这个例子就是它不能工作的一个例子。 #include <stdio.h>
int* selectionSort(int a_count, int *a);
int main(void)
{
int a[] = {4,2,3,4,4,9,98,98,3,3,3,4,2,98,1,98,98,1,1,4,98,2,98,3,9,9,3,1,4,1,98,9,9,2,9,4,2,2,9,98,4
我是一名计算机科学专业的二年级学生,目前正在学习Java,我们最近开始学习泛型。我有一个任务,我得到了一个使用泛型的排序算法列表,任务是使用它们对整数(而不是原始整数)列表进行排序。因为排序类使用扩展了比较的泛型,所以我认为简单地将Integer数组传递给它们是没有问题的,但是构建输出总是出现不兼容的类型。
相关代码如下;
主程序的一部分
final int NUMITEMS = 100000;
Integer[] list = new Integer[NUMITEMS];
int dataSize = 0;
//method reads contents of a file into ar
我使用Java进行了一个实验,以确定哪种排序方法的(冒泡或选择)运行时更快。该程序提示用户输入一个数字n,它是要排序的数组中的项数。然后创建并排序500个这样大小的数组,并使用这两种排序方法来获得排序的平均时间。我使用500、1000和2500作为n的测试输入。我下面的结果表明,选择排序比冒泡排序运行得更快,但是这两种算法的时间复杂度都是O(n^2),那么为什么选择排序运行得更快呢?
TimeBubbleSort类
public class TimeBubbleSort {
public static void main(String[] args) {
System
我在Python中看到了以下选择排序算法的代码,我的问题更多地是与python的功能有关,而不是与算法有关。
def Selectionsort(A):
for i in range (0,len(A)-1):
minIndex=i
for j in range (i+1,len(A)):
if A[j]<A[minIndex]:
minIndex=j
if minIndex !=i:
A[i],A[minIndex]=A[minIndex],A[i]
A=[10,7,6,4,5,3,1,8,2,9]:
每当我尝试运行我的程序时,它都会返回SIGABRT错误。但是,如果我尝试在randomNums1的大小设置为不是98 (即99或49)的情况下运行它,程序会执行得很好。下面是我的函数代码,然后是对函数的调用。为什么这只适用于某些大小的randomNums1,而不适用于所有? using namespace std;
int SelectionSort(int numbers[], int numbersSize) {
int counter = 0;
int i;
int j;
int indexSmallest;
int temp;
fo
我有一个关于将数组的第一个元素的地址传递给(递归)函数的问题:
selectionSort( &b[1], size-1);
当地址传递给函数时,我知道函数参数必须是一个指针。selectionSort方法将int b[]作为参数。而不是指针。
但是代码运行起来没有任何问题。这段代码还生成子数组。当我们传递第一个元素时,它会成为子数组的零元素吗?
void selectionSort(int b[], int size)
{
int temp,i;
if (size>=1)
{
for (i = 0; i < size; i++)
我从一个关于选择排序的网站上得到了这个方法,我需要检查它是如何工作的:
import java.util.Arrays;
public class SelectionSort {
public static void selectionSort(int[] data, int low, int high) {
if (low < high) {
swap(data, low, findMinIndex(data, low));
selectionSort(data, low + 1, high);
}
}
public static v
下面是我的main代码,它使用nanoTime()计算selectionSort()的时间差:
System.out.println("Start time :: " + test.start());
test.selectionSort();
System.out.println("End time :: " + test.end());
System.out.println(test);
System.out.print("Time took to run selectionSort() == ");
那么,为什么这段代码不起作用呢?它总是返回原始列表(我没有放入return语句,但是有人能确定为什么我的选择排序算法背后的逻辑不起作用吗)。如有任何帮助,我们将非常感谢!
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class ArrayListDemo {
public static void main(String [] args) {
ArrayList <String> list = new ArrayList <S
我用java编写了一个选择排序代码。我知道它非常基本的算法,但由于我正在学习,所以想要你的输入关于代码的质量。请看代码:包selection_sort;
import java.util.Scanner;
public class SelectionSort {
int [] arrayToBeSorted;
Scanner scan=new Scanner(System.in);
SelectionSort(){
System.out.println("Enter the number of elements");
int total=scan.nex
下面是我的c++代码。我试图使用指针(开始和结束)实现一个选择排序。代码会编译,但在对随机生成的列表进行排序之前,我得到了一个分段错误(目前只打印随机数)。
任何帮助,这是为什么和如何解决它,将不胜感激。
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
using namespace std;
void selectionSort(int *start, int *stop) {
for (int i = *start; i < *stop - 1; ++i) {
i
我想把下面的JAVA代码转换成python (我是Python的初学者)
public void selectionSort(int[] arr) {
int i, j, minIndex, tmp;
int n = arr.length;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[minIndex])
我试图在一个JUnit类上理解和应用黑盒/白盒SelectionSort技术,但我很难理解该采取什么方向。
我尝试从我的SelectionSort类中测试数组的大小,但是我的方法(unsortedArray)没有被识别。
@Test
public void testUnsortedArray() {
int n = 20;
int[] x = new int[n];
for (int i = 0; i < 20; i++) {
x[i] = (n);
n--;
i++;
}
SelectionSor
我正在尝试对图书图书馆使用选择排序,这样它就可以按字母顺序对它们进行排序,但是我无法使用它。
SelectionSort(库);不工作,但SelectionSort(标题);工作,有什么想法吗?谢谢:)
下面是完整的代码:
using System;
using System.Collections.Generic;
using System.Text;
namespace BookSortingEx
{
class Program
{
static void swap<T>(ref T x, ref T y)
{
当我运行普通构造函数的驱动程序时,为什么我会得到这个空指针异常:
public Average()
{
int[] data = new int[5];
for(int i = 0; i < data.length; i++)
{
score = input.nextLine();
int value = Integer.parseInt(score);
data[i] = value;
}
selectionSort();
calculateMean();
}
在调用calculat
我很难将我的数组列表作为仿真器发送到一个通用方法。你知道我在这里做错了什么吗?
ArrayList<Integer> list50k = new ArrayList<>();
list50k.add(1);
list50k.add(5);
list50k.add(8);
list50k.add(7);
selectionSort(list50k); // error is in this line
然后,我尝试将数组发送到该方法,并得到一个错误。以下是一种方法:
public static <T extends Comparable<T>> v
我在VB.NET中有这样一段代码:
' This code will sort array data
Public Sub SelectionSort(ByVal array as ArrayList)
For i as Integer = 0 To array.Count -1
Dim index = GetIndexMinData(array, i)
Dim temp = array(i)
array(i) = array(index)
array(index) = temp
Next
End Sub
Public Fu
我正在试着用bool写SelectionSort。
void ss( int tab[], int n )
{
bool flag;
for( int i = 0; i < n; i++ )
{
int min = tab[ i ];
for( int j = i + 1; j < n; j++ )
{
if( min > tab[ j ] )
{
flag = true;
tab[ j ] =
我有一个处理通过selectionSort对数组进行排序的类。。.I在我的main函数(附加在底部)中对数组进行排序时遇到问题。...What是调用selctionSort()的正确方法
我的问题:“类型SortArray中的方法selectionSort(T[], int)不适用于参数(int[], int)”...我试图将int数组传递给函数,它总是给我这个错误。
/**
Class for sorting an array of Comparable objects from smallest to
largest.
*/
public class SortArray
{
我很难调用我的SelectionSort类,如下所示。我得到一个错误“不能访问SelectionSort”。我试图了解SelectionSort类排序随机数组所需的时间。下面是SelectionSort类:
import java.lang.*;
public class SelectionSort {
public static <T extends Comparable<T>> void sort(T[] a) {
selectionSort(a, a.length-1);
}
private static <T extends Com
这是用C编写的简单代码(选择排序):
#include <stdio.h>
#include <stdlib.h>
#define ItemCount 10
int numbers[ItemCount] = {4,9,3,7,2,5,10,2,12,6};
int MinNumberIndex(int start) {
int i;
int minindex = start;
for (i = start + 1; i < ItemCount; i++)
if (numbers[minindex] > n
我的算法不起作用,我是所有算法和javascript的新手,我只是不知道为什么它不起作用。
这里是数组(23,19,35,12,30)
最大值= 0
这是我拥有的..。
For K= 0 to n – 2
MAX = 0
For j = 0 to n – k - 2
If item[j] > item[MAX]
MAX = item[j]
end if
end for
swap n - k -
我在网上找到了用于选择排序的代码:
def selectionSort(alist):
for fillslot in range(len(alist)-1,0,-1):
positionOfMax=0
for location in range(1,fillslot+1):
if alist[location]>alist[positionOfMax]:
positionOfMax = location
temp = alist[fillslot]
ali