How to sort an array in C++ for beginners ➡️

preview_player
Показать описание
#array #sort

How to sort an array in C++ tutorial example explained
Рекомендации по теме
Комментарии
Автор

#include <iostream>

void sort(int array[], int size);

int main()
{
int array[] = {10, 1, 9, 2, 8, 3, 7, 4, 6, 5};
int size =

sort(array, size);

for(int element : array){
std::cout << element << " ";
}

return 0;
}
void sort(int array[], int size){
int temp;
for(int i = 0; i < size - 1; i++){
for(int j = 0; j < size - i - 1; j++){
if(array[j] < array[j + 1]){
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}

BroCodez
Автор

I finally got this! can do it every time now! next step is actually understanding the theory behind this, beacause i feel like i have just memorized what to write...

lastwaveundergroundsaviour
Автор

We can just use swap in the end. Thx for video !

void bubbleSort(int arr[], int size)
{
int i, j;
for (i = 0; i < size - 1; i++)

// Last i elements are already
// in place
for (j = 0; j < size - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(arr[j], arr[j + 1]);
}

newLevel_
Автор

Oh man, the first difficulties! It was so easy until now :)

PseudoPolish
Автор

holy snap im gonna have to watch this a bunch of times to fully understand. But cool video u the goat man!!

lastwaveundergroundsaviour
Автор

thanks for the lessons, Bro.

shouldn't we use:
j < size - i - 2; in our inner loop?

because on our first iteration when i = 0, we compare j[size - 1] with j[size](added one) which seems like out of range for me. will it compare to garbage value in this case?

ghostintheshelf
Автор

#include <algorithm> // for std::sort
#include <iostream>

int main() {
int arr[] = {5, 2, 8, 1, 3};
int size = sizeof(arr) / sizeof(arr[0]);

//std::sort(arr, arr + size);
// Descending
std::sort(arr, arr + size, std::greater<int>());


std::cout << "Sorted array: ";
for (int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;

return 0;
}

Simple way

gwihsanz
Автор

alternative version with one for and one while:
void sort(int nums[], int size){
bool swapped = true;
while(swapped){
swapped = false;
for(int i = 0; i < size - 1; i++){
if(nums[i] > nums[i + 1]){
// swap
int temp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = temp;
swapped = true;
std::cout << "swapped\n";
}
}
}
}

theofrese
Автор

that was tough especially the nested loop i'll stare on it for couple minuets until i 100% get it

manes
Автор

sorry but i dont understand why you put size -1 and then size-i-1??
i tried to use size in the two loop and it work its the same??
thanks for your answer .)

giuliocarchidi
Автор

Why is there additional 0 on top when I run it in ascending order but the 0 not comes while I run in descending order?

ayushraj
Автор

Decided to figure out sorting algorithm before watching the video:
int main(){
int temp;
int counter;
int myRandArray[] = {5, 2, 1, 3, 6, 4, 8, 7, 9, 10};
do{
counter = 0;
for(int i = 0; i < sizeof(myRandArray) / sizeof(myRandArray[0]) - 1; i++){
if(myRandArray[i] > myRandArray[i + 1]){
temp = myRandArray[i + 1];
myRandArray[i + 1] = myRandArray[i];
myRandArray[i] = temp;
}
else{
counter++;
}
}
}while(counter != sizeof(myRandArray) / sizeof(myRandArray[0]) - 1);

for (int i = 0; i < sizeof(myRandArray) / sizeof(myRandArray[0]); i++){
std::cout << myRandArray[i] << '\n';
}

return 0;
}
It's probably better to use a nested for loop

Гэроймайдана
Автор

I gave a shot at making the sort function before watching the video.

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

std::cout << "Not Sorted Array: " << "\n";
for (int j = 0; j < (sizeof(numbers) / sizeof(int)); j++)
{
std::cout << numbers[j] << "\n";
}

for (int i = 0; i < (sizeof(numbers) / sizeof(int)) - 1;)
{
int k = i;

while (numbers[k] > numbers[k + 1] && k < (sizeof(numbers) / sizeof(int) - 1))
{
int temp = numbers[k];
numbers[k] = numbers[k + 1];
k++;
numbers[k] = temp;

if (i != 0)
i = 0;
}

if (numbers[i] > numbers[i + 1])
{
continue;
}
else
{
i++;
}

}

std::cout << "Sorted Array: " << "\n";
for (int j = 0; j < (sizeof(numbers) / sizeof(int)); j++)
{
std::cout << numbers[j] << "\n";
}

xXSLEEPSTERXx