2
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
int q = Convert.ToInt32(Console.ReadLine());
string arrays = Console.ReadLine();
string[] arraysint = new string[q];
arraysint = arrays.Split(' ');
int[] numbers = Array.ConvertAll(arraysint, int.Parse);
int i;
int save = 0;
int j;
int temp;
int current = q - 1;
int[] maior = new int[1];
//int[] numbers = new int[q];
for (j = 0; j < q; j++)
{
for (i = 0; i < current + 1; i++)
{
if (current == 1)
{
goto final;
}
if (i == current)
{
if (current == save)
{
current--;
}
if (current < q - 1)
{
if (maior[0] == numbers[current] && maior[0] != 0)
{
current--;
}
}
temp = numbers[current];
numbers[save] = temp;
numbers[current] = maior[0];
current--;
}
if (maior[0] < numbers[i])
{
save = i;
maior[0] = numbers[i];
}
}
maior[0] = 0;
}
final:
Console.Clear();
for (i = 0; i < q; i++)
{
Console.Write(numbers[i] + " ");
}
Console.ReadKey();
}
}
}
input:
5
5 4 3 2 1
Output:
1 3 2 4 5
I’ve done everything, but I can’t fix it. Somebody help me there?
You’ve made it very difficult, considering Bubble Sort are 2
for
and 1if
with aswap
, literally 6 lines of code. Several things don’t make much sense, such as using an array with a house for themaior
when you can use a normal variable. Even the notion ofmaior
for this algorithm is not necessary. The same can be said forgoto
, which makes it difficult to visualize the flow of the program. I would personally advise you to start again, calmly and think about the logic required for the algorithm, which is much simpler than what you have at the moment.– Isac