Решил поделится кодом программы сортирующей значения массива.[ #include <iostream > #include <cstdlib> using namespace std; void main() { setlocale(LC_ALL, "ru"); int a, b, t, size; size = 10; int nums[10]; for (t = 0; t < size; t++)nums[t] = rand();//заполнение массива случайными значениями for (a = 1; a < size - 1; a++)//сама сортировка for (b = size - 1; b >= a; b -- ){ if (nums[b - 1] > nums[b]){ t = nums[b - 1]; nums[b - 1] = b; nums[b] = t; } } for (t = 0; t < size; t++)cout << nums[t] << " ";//вывод в консоль } Код #include <iostream > #include <cstdlib> using namespace std; void main() { setlocale(LC_ALL, "ru"); int a, b, t, size; size = 10; int nums[10]; for (t = 0; t < size; t++)nums[t] = rand();//заполнение массива случайными значениями for (a = 1; a < size - 1; a++)//сама сортировка for (b = size - 1; b >= a; b -- ){ if (nums[b - 1] > nums[b]){ t = nums[b - 1]; nums[b - 1] = b; nums[b] = t; } } for (t = 0; t < size; t++)cout << nums[t] << " ";//вывод в консоль }
#include <iostream> #include <algorithm> using namespace std; int main(){ int mas[5]={1,3,2,5,0}; sort(mas,mas+5); for(int i=0;i<5;i++){ cout<<mas[i]<<" "; } } output:0 1 2 3 5 Вот тебе способ быстрой сортировки, она куда быстрее чем твоя(пузырьковая) Код #include <iostream> #include <algorithm> using namespace std; int main(){ int mas[5]={1,3,2,5,0}; sort(mas,mas+5); for(int i=0;i<5;i++){ cout<<mas[i]<<" "; } } output:0 1 2 3 5 Вот тебе способ быстрой сортировки, она куда быстрее чем твоя(пузырьковая)
сортировка вставками вроде лучше пузырька #include <iostream > #include <cstdlib> using namespace std; int i, j, key = 0, temp = 0; void InsertSort(int *mas, int n) //сортировка вставками { for (i = 0; i < n - 1; i++) { key = i + 1; temp = mas[key]; for (j = i + 1; j > 0; j--) { if (temp < mas[j - 1]) { mas[j] = mas[j - 1]; key = j - 1; } } mas[key] = temp; } cout << endl << "Результирующий массив: "; for (i = 0; i < n; i++) //вывод массива cout << mas[i] << " "; } //главная функция void main() { setlocale(LC_ALL, "Rus"); int n; cout << "Количество элементов в массиве > "; cin >> n; int *mas = new int[n]; for (i = 0; i < n; i++)mas[i] = rand(); InsertSort(mas, n); //вызов функции delete[] mas; system("pause>>void"); } Код #include <iostream > #include <cstdlib> using namespace std; int i, j, key = 0, temp = 0; void InsertSort(int *mas, int n) //сортировка вставками { for (i = 0; i < n - 1; i++) { key = i + 1; temp = mas[key]; for (j = i + 1; j > 0; j--) { if (temp < mas[j - 1]) { mas[j] = mas[j - 1]; key = j - 1; } } mas[key] = temp; } cout << endl << "Результирующий массив: "; for (i = 0; i < n; i++) //вывод массива cout << mas[i] << " "; } //главная функция void main() { setlocale(LC_ALL, "Rus"); int n; cout << "Количество элементов в массиве > "; cin >> n; int *mas = new int[n]; for (i = 0; i < n; i++)mas[i] = rand(); InsertSort(mas, n); //вызов функции delete[] mas; system("pause>>void"); }