Answer the question
In order to leave comments, you need to log in
Is there a difference in the implementation of the sorting algorithm?
I started taking the CS50 course, after watching the lecture I wrote a bubble sort algorithm, my version:
int array[]={2,4,5,9,1,0,7,6,3,8};
int counter;
do
{
counter=0;
for(int i=0; i < sizeof(array)/sizeof(array[0])-1; i++)
{
if(array[i]>array[i+1])
{
int temp=array[i];
array[i]=array[i+1];
array[i+1]=temp;
counter++;
}
}
}
while (counter>0);
for(i = 0 ; i < n - 1; i++) {
// сравниваем два соседних элемента.
for(j = 0 ; j < n - i - 1 ; j++) {
if(a[j] > a[j+1]) {
// если они идут в неправильном порядке, то
// меняем их местами.
int tmp = a[j];
a[j] = a[j+1] ;
a[j+1] = tmp;
}
}
}
Answer the question
In order to leave comments, you need to log in
There is no fundamental difference, only the variable names are different and while instead of for
But do you know that this is one of the slowest algorithms?
https://www.youtube.com/watch?v=ZZuD6iUe3Pc
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question