C sort an array 💱

preview_player
Показать описание
C sort an array program tutorial example explained

#C #sort #array
Рекомендации по теме
Комментарии
Автор

#include <stdio.h>

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

void printArray(char array[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%c ", array[i]);
}
}

int main()
{
//int array[] = {9, 1, 8, 2, 7, 3, 6, 4, 5};
char array[] = {'F', 'A', 'D', 'B', 'C'};
int size =

sort(array, size);
printArray(array, size);

return 0;
}

BroCodez
Автор

If you do not use the condition j < size - i - 1 in the inner loop and the outer loop does not run until i < size - 1, the program will not sort the array correctly. Here's what will happen in different scenarios:

Inner Loop Without j < size - i - 1
If you use the condition j < size instead of j < size - i - 1 in the inner loop, you will encounter two primary issues:

Out-of-Bounds Access: When j reaches size - 1, array[j + 1] will access an element beyond the end of the array, leading to undefined behavior.
Inefficiency: The inner loop will make unnecessary comparisons and swaps even after the largest elements have bubbled to the end of the array.
Outer Loop Without i < size - 1
If you use the condition i < size instead of i < size - 1 in the outer loop, the program will make an unnecessary extra pass through the array. This extra pass won't harm the correctness but will reduce efficiency slightly.

GameReviews
Автор

Bro code, I had learnt python, html/css, JavaScript and now currently learning this beautiful playlist of c language . Thanks man
May God bless you with more knowledge

blenderofficial
Автор

for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - i - 1; j++)


Example Walkthrough with Array [5, 3, 8, 4, 2]
Initial State of the Array:

[5, 3, 8, 4, 2]
First Pass (i = 0):

Outer loop starts with i = 0.
Inner loop runs with j ranging from 0 to 3 (size - i - 1 = 5 - 0 - 1 = 4).
Comparisons and swaps within this pass:
j = 0: Compare 5 and 3, swap → [3, 5, 8, 4, 2]
j = 1: Compare 5 and 8, no swap → [3, 5, 8, 4, 2]
j = 2: Compare 8 and 4, swap → [3, 5, 4, 8, 2]
j = 3: Compare 8 and 2, swap → [3, 5, 4, 2, 8]
End of first pass: largest element 8 is at the end.
Second Pass (i = 1):

Outer loop continues with i = 1.
Inner loop runs with j ranging from 0 to 2 (size - i - 1 = 5 - 1 - 1 = 3).
Comparisons and swaps within this pass:
j = 0: Compare 3 and 5, no swap → [3, 5, 4, 2, 8]
j = 1: Compare 5 and 4, swap → [3, 4, 5, 2, 8]
j = 2: Compare 5 and 2, swap → [3, 4, 2, 5, 8]
End of second pass: second largest element 5 is in position.
Third Pass (i = 2):

Outer loop continues with i = 2.
Inner loop runs with j ranging from 0 to 1 (size - i - 1 = 5 - 2 - 1 = 2).
Comparisons and swaps within this pass:
j = 0: Compare 3 and 4, no swap → [3, 4, 2, 5, 8]
j = 1: Compare 4 and 2, swap → [3, 2, 4, 5, 8]
End of third pass: third largest element 4 is in position.
Fourth Pass (i = 3):

Outer loop continues with i = 3.
Inner loop runs with j ranging from 0 to 0 (size - i - 1 = 5 - 3 - 1 = 1).
Comparisons and swaps within this pass:
j = 0: Compare 3 and 2, swap → [2, 3, 4, 5, 8]
End of fourth pass: fourth largest element 3 is in position.
After these passes, the array is sorted: [2, 3, 4, 5, 8].

Summary
i: Controls the number of passes. Each pass ensures the next largest unsorted element is moved to its correct position at the end.
j: Controls the comparisons within each pass. Its range decreases with each pass as the largest elements are sorted to the end.
Bubble Sort Mechanism: The largest unsorted element "bubbles up" to its correct position during each pass of the outer loop.
Visualization
Initial Array: [5, 3, 8, 4, 2]
After Pass 1: [3, 5, 4, 2, 8]
After Pass 2: [3, 4, 2, 5, 8]
After Pass 3: [3, 2, 4, 5, 8]
After Pass 4: [2, 3, 4, 5, 8]
The sorted array is achieved after these passes, demonstrating how i and j control the sorting process.

GameReviews
Автор

Many thanks for the video.

Why Line 5 and 7 for codition: i < size -1
and Line 21 for condition: i < size

bytecode
Автор

Did an ascending order and the first few index has random values and my inputted values are sorted starting approximately from the middle of the array size.

nikkigarcia
Автор

@03:29, you added -i to j < size -1 .. the result came out the same with or without the -1 . What do you mean by optimize? WIll it affect other things with or without optimization?

brucebergkamp
Автор

Do you also have a video for counting sort?

mokochan
Автор

what is the reason for size-1? is it because we only compare up to the last 2 element? if we go one more, the last element wouldn't have anything to compare itself to??

prumchhangsreng
Автор

Thanks for being awesome << hope thats a thumbs up dude LOL

Garrison
Автор

I did not understand why the for loop goes from 0 to size-1 with strict disequality, this means it goes from 0 (first element) to the n-2 element since if size = 5 elements of the array goes from index 0 to 4 so size-1=4 but with the "<" it goes from 0 to 3 because at size = 4 it stops without executing the loop, why's that?

richardfeynman
Автор

I dont really understand why we needed a nested loop ( without it the loop will be messed up )

stnoverheaven
Автор

Hey I was wondering how you are able to modify an array inside a function and pass it to the next function without returning the modified array from the first, if someone could explain, that would be dope.

PoptartWisard
Автор

How were you able to get away with having temp still as an integer when the data type being swapped is char?

vsots
Автор

pls u are too fast for beginners like me, but still thanks ur vid is great

babiladyland
Автор

in swap section, why don't we use this ( array[j] ^= array[j + 1] ^= array[j] ^= array[j + 1]; ) instead of this /*
int temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;
*/

CS_Mustcode
Автор

Hey Bro, what is this kind of sort name?

KTL-rsge
Автор

Line 12 and 13 at 4:07 of the video. Why do I get a warning for using strcpy()?

cryptokd
Автор

i am not a profecinal coder but i think there is no need of i loop

akhilmasanam
Автор

Here's a bubble sort I made:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

void bubbleSort(int myArray[], int arraySize);
void printArray(int myArray[], int arraySize);

int main(){
int myArray[] = {1, 3, 5, 11, 12, 800, 2, 6, 4, 8, 9, 10, 17, 7};
int arraySize =

bubbleSort(myArray, arraySize);
printArray(myArray, arraySize);

return 0;
}

void bubbleSort(int myArray[], int arraySize){
int temporary;
for(int i = 0; i < arraySize - 1; i++){
for(int j = 0; j < arraySize - 1 - i; j++){
if(myArray[j] > myArray[j+1]){
temporary = myArray[j];
myArray[j] = myArray[j+1];
myArray[j+1] = temporary;
}
}
}
}

void printArray(int myArray[], int arraySize){
for(int i = 0; i < arraySize; i++){
printf("%d\n", myArray[i]);
}
} // This was incredibly easy for me because I already watched brocode's c++ videos and this is basically the same thing.

PSIwolf