选择排序与交换两个元素 [英] Selection sort with swapping two elements

查看:88
本文介绍了选择排序与交换两个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我必须找到最小值和第二个最小值,而不是仅找到最小值,并将其存储在正确的位置。

我的代码在所有情况下都不起作用

例如

So,instead of finding the minimum only,I have to find the minimum value and the second minimum value and store it in the right place.
my code isnt working in all cases
for example

#include <iostream>
void AdvancedSelectionSort(int data[], int n)
{ 
	for(int i=0;i<n-1;i=i+2){
		int smallest,secondsmallest;
		 
	  smallest=i;
	  secondsmallest=i+1;


	for(int j=i+1;j<n;j++)
	{
		
		if(data[j]<=data[smallest]){
	    secondsmallest=smallest;
		smallest=j;}

		else if(data[j]<=data[secondsmallest])
			secondsmallest=j;
	}

	
	  
	if(secondsmallest!=(i+1))
		Swap(data[i+1],data[secondsmallest]);

	if(smallest!=i)
		Swap(data[i],data[smallest]);

	
	
	
	
	}
}


int main(){
	int x[10]=[2,5,8,7,9,12,88,74,3,10]
	AdvancedSelectionSort(x,10);
	for(int i=0;i<10;i++)
		cout<<x[i]<<" ";



}







我的尝试:



void Swap(int& a,int& b)



{int temp = a;

a = b;

b = temp;}





void AdvancedSelectionSort(int data [],int n)

{

for(int i = 0; i< ; n-1; i = i + 2){

int最小,最小;



最小= i;

secondsmallest = i + 1;





for(int j = i + 1; j< n; j ++)

{



if(data [j]< = data [smallest]){

secondsmallest = minimum;

最小= j;}



否则if(data [j]< = data [secondsmallest])

secondsmallest = j;

}







if(secondsmallest !=(i + 1))

交换(数据[i + 1],数据[最小最小]);



if(最小!= i)

交换(数据[i],数据[小est]);











}

}




What I have tried:

void Swap(int &a,int&b)

{int temp=a;
a=b;
b=temp;}


void AdvancedSelectionSort(int data[], int n)
{
for(int i=0;i<n-1;i=i+2){
int smallest,secondsmallest;

smallest=i;
secondsmallest=i+1;


for(int j=i+1;j<n;j++)
{

if(data[j]<=data[smallest]){
secondsmallest=smallest;
smallest=j;}

else if(data[j]<=data[secondsmallest])
secondsmallest=j;
}



if(secondsmallest!=(i+1))
Swap(data[i+1],data[secondsmallest]);

if(smallest!=i)
Swap(data[i],data[smallest]);





}
}

推荐答案

如果您的任务是对数据进行排序,然后获取最小的两个元素,那么请停止查找最小和最小的,并专注于使工作的种类。删除所有最小的代码,然后排序。

当你有这个工作时,最小的两个是结果数组的第一个和第二个元素...



如果你不打算对它进行排序,那么你可以在一次通过数据中找到最小和第二小的变量:创建一个最小和第二个最小变量,并将它们都设置为最大值整数可以保持。

传递数据并将每个值(V)与第二个最小值(S2)进行比较。如果它更小,则将其与最小值(S1)进行比较。然后:

如果V> = S2忽略它。

如果V< S2和V> = S1将S2设定为V.

如果V < S2和V < S1将S2设置为S1,将S1设置为V.

在循环结束时,您具有最小和最小的。
If you task is to sort the data, and then get the smallest two elements, then stop looking for the smallest and the second smallest, and concentrate on getting the sort to work. Remove all "smallest" code, and just sort.
When you have that working, the smallest two are the first and second elements of the resulting array...

If you aren't supposed to sort it, then you can find the smallest and second smallest in a single pass through the data: Create a smallest and secondSmallest variables, and set them both to the maximum value an integer can hold.
Pass through the data and compare each value (V) with the second smallest (S2). If it's smaller, compare it to the smallest (S1). Then:
if V >= S2 ignore it.
if V < S2 and V >= S1 set S2 to V.
if V < S2 and V < S1 set S2 to S1, set S1 to V.
At the end of the loop, you have the smallest and second smallest.


for(int i=0;i<n-1;i=i+2){
    int smallest,secondsmallest;

  smallest=i;
  secondsmallest=i+1;



想想看:当数据[i]不小于数据[i + 1]时会发生什么?



当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器来看看你的代码在做什么。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



调试器在这里向您展示您的代码在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个bug。



[更新]

顺便说一下:


Think about it: what happen when data[i] is not smaller than data[i+1] ?

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Update]
By the way:

int x[10]=[2,5,8,7,9,12,88,74,3,10]



缺少;在行尾。



试用:


is missing the ";" at the end of line.

Try with:

int x[10]=[2,1,4,3,6,5,8,7,10,9];


$ b当数据[i]不小于数据[i + 1]


to see the problem when data[i] is not smaller than data[i+1]


这篇关于选择排序与交换两个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆