冒泡按字母顺序排列字符 [英] Bubble sort the characters alphabetically

查看:83
本文介绍了冒泡按字母顺序排列字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写并运行一个C ++程序,该程序具有一个名为dB []的20元素数据库(数组),其元素如下所示:



Write and run a C++ program that has a 20-element database (array) called dB[ ] with the elements declared as shown below:

const int NUM_ELE = 20;
char dB[NUM_ELE] = { 'x','b','A','X','w','T','M','e','n','Z','z','d','Q','L','K','j','S','v','q','R'};





提供一个名为bub_sort()的函数,它使用冒泡(交换)排序对元素进行重新排序(升序中的大写字母首先是字母顺序,然后是按升序字母顺序的小写字母。)此函数必须跟踪重新排序数组中的移动总数,并在函数return中提供该数字。该函数必须将dB []传递给它(通过引用)以及数据库中的元素数量(按值)。在main()中调用该函数后,显示所需的移动总数,并显示20个元素中每个元素的重新排序数组(表示每个元素的INDEX编号)。请务必妥善记录您的程序并提供描述性显示。







我收到按字母顺序排列的订单,但我如何计算总移动次数?



我尝试过的方法:





Provide a function called bub_sort() that uses a bubble (exchange) sort to reorder the elements (capitals in ascending alphabetic order first, then lower-case letters in ascending alphabetic order.) This function must track the total number of moves in reordering the array and provide that number in the function "return." The function must have dB[ ] passed to it (by reference) along with the number of elements in the database (by value). AFTER the function is called in main(), display the total number of moves required and show the reordered array for each of the 20 elements (indicate the INDEX number for each element). Be sure to properly document your program and provide a descriptive display.



I got the alphabetical orders, but how do I count the total number of moves?

What I have tried:

#include<iostream>                       //library needed
using namespace std;					 //Library location

int bubbleSort(char[], char);

int main()									////Driver function header
{
	const int NUM_ELE = 20; 					// variable declaration
	char dB[NUM_ELE] = { 'x','b','A','X','w','T','M','e','n','Z','z','d','Q','L','K','j','S','v','q','R'};
	int i, moves;
	char temp;


	// title display
	cout<< 	"	This program sort the the 20 elements in alphabetic order"<<endl;

	for(int i = 0; i < NUM_ELE; i++)
		for(int j = 0; j < NUM_ELE-1; j++)
		{
			if(dB[j+1] < dB[j])
			{
				temp = dB[j];
				dB[j] = dB[j+1];
				dB[j+1] = temp;
			}
		}

	for(int i = 0; i < NUM_ELE; i++)
		cout<<dB[i] <<" ";

	return 0;
}

推荐答案

你有一个名为'move'的变量..好的,需要初始化为0



然后考虑一下你的冒泡行为发生的位置(交换元素) - 不是一个增加'移动'的好地方吗?



最后,您可以在将已排序的数组写入控制台之后(或之前)打印(或其他)变量'move'的内容
you have a variable called 'moves' .. ok, that needs to be initialised to 0

then think about where the action is happening in your bubble sort (swapping elements) - isnt that a good place to increment 'moves' ?

lastly you can print (or whatever) the contents of the variable 'moves' after (or before) you write the sorted array to the console


您是否编写此代码?

代码不符合作业要求。它不使用函数也不计算移动。

如何编写冒泡排序并且无法计算移动数?这是要求的简单部分。



看起来你有很好的技能在互联网上找东西,但你走错了轨道。



我建议你自己做作业。解决这些问题将有助于您学习编程。你的失败是帮助你的老师发现你的弱点并采取补救措施。

当你不明白的时候,问你的老师。



顺便说一下,程序编码很差,并且完成工作所需的工作量约为2倍。
Did you write this code ?
The code don't respect the requirement of the assignment. It don't use a function nor count the moves.
How can you program a bubble sort and be unable to count the moves ? it is the easy part of the requirement.

Looks like you have a good skill at finding things on internet, but you follow the wrong track.

I recommend you to do your assignments by yourself. Solving these problems will help you in the process of learning programming. Your fails are helping your teacher to spot your weakness and apply remedial actions.
When you don't understand something, ask your teacher.

By the way the program is poorly coded and do about 2 times the amount of work necessary to do the job.


这篇关于冒泡按字母顺序排列字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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