我是一个编程的初学者,只是在玩排序,并制作了这个算法。它类似于冒泡,但它不是比较相邻的对,而是像这样的对:第一和第二,第一和第三……第二和第三,第二和第四,以此类推。您能告诉我该算法的性能/效率是什么吗?或者将其与气泡进行比较?或者至少建议我如何自己解决这个问题。我感兴趣的是有多少气泡比这个更好。谢谢。
void sortArray(int a[]) {
int q, x, temp;
for ( q = 0; q < SIZE - 1; q++ ) {
for ( x = q + 1; x < SIZE; x++ ) {
if (a[q] < a
我正在为软件开发人员的面试做准备,并且一直在研究算法问题。我的书展示了一个Heapsort算法,它可以按递增的顺序对无序数组进行排序。我试图修改它,这样它就可以与最小堆进行排序。但是,当我遵循代码中的逻辑时,它不能正确地排序数组。我的代码(伪代码)有什么问题?
The array to be sorted: 16, 14, 10, 8, 7, 9, 3, 2, 4, 1
这本书的堆排序算法使用max-heapify:
HEAPSORT(A)
BUILD-MAX-HEAP(A)
for i = A.length down to 2
swap A[1] with A[i]
通过理解插入排序算法,我编写了这段代码。我的老师说它是冒泡排序,但我的朋友说它是插入的。有没有人可以检查一下并向我简要介绍一下。
#include <stdio.h>
void sort(int n) {
int i, j;
float arr[n], k;
for (i = 0; i <= n - 1; i++) {
printf("Enter the number");
scanf("%f", &arr[i]);
}
for (i = 1; i <= n - 1; i++) {
j
我正在尝试编写一个程序来计算冒泡排序和插入排序的运行时间。它接受两个输入,元素和元素的数量,并计算它们的运行时。这就是我到目前为止所拥有的,但两个分类器的打印时间是相同的。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
int bubblesort(int a[], int n);
int insertionsort(int a[], int n);
int main()
{
int s,temp,i,j,comparison
更新:好的,我看到它是一个冒泡排序,但是它的效率是不是因为在特定的运行中没有交换时不会停止?它会一直运行到first为空。
嗨,我有一个排序算法如下所示。我的问题是,这是哪种排序算法?我认为它是冒泡排序,但它不做多次运行。有什么想法吗?谢谢!
//sorting in descending order
struct node
{
int value;
node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t
我想要一些帮助来解决我在教科书中发现的以下问题。
sort (array[], nr_of_item)
{
while(true)
i:=value from an n-sided fair dice roll
j:=value from an n-sided fair dice roll
if (i > j)
swap i and j
if (array[i] > array [j])
swap array[i] and array[j]
end while
}
现在,它说它没有描述一个正确的算法
我对Java很陌生,并且试图给我的Bubblesort算法和快速排序算法计时,看看哪种算法更快!我已经做了一些研究,并且知道在Java中为您的代码计时,您可以使用System.currentTimeMillis()。似乎不能让我的System.currentTimeMillis()工作的时间泡沫或快速,任何帮助将是非常感谢的。
// Class implementing an array based list.
// Bubblesort and quicksort algorithms are implemented also.
class ArrayList
{
private stat
我正在创建一个冒泡排序程序,它对数组中的随机整数进行排序。该数组应该能够容纳多达一百万个已排序的整数。当我达到一个较高的数字(例如,250,000)时,程序将停在那里,永远不会输出任何内容。代码如下:
using System;
namespace SortingProject
{
class MainClass
{
public static void Main(string[] args)
{
//create an array to hold integers
int[] list = n
我正在尝试找到一种方法,根据输入的姓氏对这个单链表进行排序。我想我可能会尝试冒泡排序,但我在通过第二个元素遍历列表并进行比较时遇到了问题。该列表现在只包含3个名称作为控制台条目,但当我完成此操作时,它应该有10个名称。任何帮助都将不胜感激。
package LinkedList;
import java.util.*;
class SLinkedList
{
public String data1;
public String data2;
public SLinkedList next;
public SLinkedList()
{
dat
我已经编写了一个冒泡排序程序,将10000个唯一值按顺序排序。
我已经运行了这个程序,它给了我一个输出,但是这个输出看起来好像不太对劲。
代码如下:
public class BubbleSort {
public static void main(String[] args) {
int BubArray[] = new int[]{#here are 10000 integers#};
System.out.println("Array Before Bubble Sort");
for(int a = 0;
我执行了以下代码来检查冒泡排序和插入排序所需的迭代和交换次数。尽管(参见下面的代码)插入排序的迭代次数和交换次数都是冒泡排序的一半,但是为什么两者都有相同的大O复杂度
static void bubbleSortExample()
{
int iterationCount=0;
int swaps=0;
int [] arr={2,6,1,4,8,7,10,3};
int temp=0;
for(int i=0; i< arr.length; i++)
{
it
下面我有气泡排序的代码。我想知道怎样才能运行得更高效,循环次数更少。 package bubbleSort;
public class BubbleSort {
public static void main(String[] args) {
// initialize array to sort
int size = 10;
int[] numbers = new int[size];
// fill array with random numbers
randomArray(
bubblesort :: (Ord a) => [a] -> [a]
bubblesort (x:y:xs)
| x > y = y : bubblesort (x:xs)
| otherwise = x : bubblesort (y:xs)
bubblesort (x) = (x)
1.我不理解代码的最后一行;我试图注释掉它,但编译器说“Non-exhaustive in function bubblesort",当我试图将它移到代码的第一行时,编译器说”模式匹配是多余的“。它使用圆括号的方式把我搞糊涂了。
2.我尝试将类型声明更改为->
嗨,我用冒泡排序检查了其他帖子,但解决方案在我的例子中不起作用:所以算法工作,直到我重复几次while循环。但是,我如何才能在不使用输入的情况下做到这一点呢?下面是我的代码,所以你知道我的意思: x = [0, 0, 1, 3, 3, 2, 2, 1, 0, 4, 5]
h = None
flag = True
while flag == True:
#flag = True
for i in range(len(x) - 1):
if x[i] > x[i + 1]:
# flag = False
h =
我用C语言实现了Shell排序,它只比Bubble排序快3倍。以下是我的排序持续时间(秒):
For list of 100 integers:
BubbleSort: 0.000333
ShakeSort: 0.000282
QuickSort: 0.000048
QuickSort_Iter: 0.000063
InsertionSort: 0.000188
ShellSort: 0.000150
For list of 1000 integers:
BubbleSort: 0.028191
ShakeSort: 0.019354
QuickSort: 0.000435
QuickSor
我有5个学生的名字和他们在3门课程上的成绩,我计算了他们在这3门课程上的平均值。但然后我需要用他们的数据(名字,3门课程的成绩和平均值)从平均值最高的课程到最低的课程排序。有人能告诉我我的代码出了什么问题吗?这是我的代码。
# include <iostream>
# include <string>
# include <iomanip>
using namespace std;
void read(string names[],double grades[][4],int n);
void average(double grades[][4], int