Comments for Hackr Blog
https://hackr.io/blog
Wed, 21 Nov 2018 20:48:37 +0000hourly1https://wordpress.org/?v=4.9.8Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Heather Williams
https://hackr.io/blog/bubble-sort-in-c#comment-779
Wed, 21 Nov 2018 20:48:37 +0000https://hackr.io/blog/?p=730#comment-779Loop invariant condition for bubble sort is that when i number of iterations right most i elements are sorted and in place.
for (i = 0 to n-1)
for (j = 0 to j arr[j+1])
swap(&arr[j], &arr[j+1]);
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Ken Wanless
https://hackr.io/blog/bubble-sort-in-c#comment-778
Wed, 21 Nov 2018 20:47:55 +0000https://hackr.io/blog/?p=730#comment-778Algorithm in C++
1) Start loop till N no. of elements.
2) Start 2nd loop till N-1 because every time highest element reaches at the last position.
3) Compare next two numbers, if they are in wrong order, swap them.
4) Else compare next two elements and repeat the 2nd loop ends and decrement 1st loop.
5) Do this till 1st loop ends.
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by James Swift
https://hackr.io/blog/bubble-sort-in-c#comment-777
Wed, 21 Nov 2018 20:47:14 +0000https://hackr.io/blog/?p=730#comment-777void bubble_sort( void* array, const int numberOfItems )
{
int firstItem = 0;
int lastItem = numberOfItems – 1;
for( int pass = 0; pass < numberOfItems; ++pass )
{
for( int i = firstItem; i 0 )
swap( array, i, i+1 );
}
–lastItem;
}
}
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Rhiannon Oldfield
https://hackr.io/blog/bubble-sort-in-c#comment-776
Wed, 21 Nov 2018 20:46:30 +0000https://hackr.io/blog/?p=730#comment-776This is the way to sort a 2-D string array in C:
void bubbleSort(char a[][100], int sz){
char temp[100];
int i,j;
for(i=0; i<sz; i++){
for(j=i+1; j 0){
strcpy(temp, a[j]);
strcpy(a[j], a[j-1]);
strcpy(a[j-1], temp);
} } }
}
main(){
char x[10][100] = {“to”,”be”,”or”,”not”,”to”,”be”,”that”,”is”,”the”,”question”};
bubbleSort(x,10);
for(int i=0;i<10;i++)
printf("%s\n",x[i]);
system("pause");
}
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Tim Liu
https://hackr.io/blog/bubble-sort-in-c#comment-775
Wed, 21 Nov 2018 20:45:26 +0000https://hackr.io/blog/?p=730#comment-775The number of exchange and the number of comparison between the elements determine the efficiency of the algorithm. Complexity of bubble sort in average and worst case are same that is O(n^2), where n is the number of elements.
The no. of comparisons in bubble sort are = n*(n-1)/2
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Roy Herring
https://hackr.io/blog/bubble-sort-in-c#comment-774
Wed, 21 Nov 2018 20:44:40 +0000https://hackr.io/blog/?p=730#comment-774It depends on the input size. If the input is small data set then this doesn’t make a huge difference. But if the size of the input is large, then it would make a difference. O(n) actually be slower than n(log n) (not necessarily), when you are looking over the time complexity.
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Agnes Ward
https://hackr.io/blog/bubble-sort-in-c#comment-773
Wed, 21 Nov 2018 20:43:31 +0000https://hackr.io/blog/?p=730#comment-773The total no. of comparisons in the bubble sort is:
(n-1) + (n-2) + (n-3) + ……….. + 3 + 2 + 1 = n*(n-1)/2, so the time complexity of bubble sort is O(n^2).
Bubble sort is the simplest algorithm so the best space complexity for bubble sort is O(1) because the only one single space is required for temp variable.
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Patricia Olive
https://hackr.io/blog/bubble-sort-in-c#comment-772
Wed, 21 Nov 2018 20:42:51 +0000https://hackr.io/blog/?p=730#comment-772In bubble sort, it requires a pair of nested loops. So, the comparison starts with (n-1) in 1st pass and with each iteration in inner loop it becomes (n-2), (n-3), and so on. Therefore total no. of comparisons will be:
(n-1) + (n-2) + (n-3) + ……….. + 3 + 2 + 1 = n*(n-1)/2
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Ian Wingfield
https://hackr.io/blog/bubble-sort-in-c#comment-771
Wed, 21 Nov 2018 20:42:08 +0000https://hackr.io/blog/?p=730#comment-771The famous real time use of the bubble sort is, we all seen in our classroom when our teacher used to say that stand up in a row and then teacher lining you all up in an ascending order of height. The bubble sort algorithm comes in use here. Teacher starts to compare the height of two adjacent students and then arrange them in their height’s ascending order. With every repeating steps of this sort, the students start standing in a more orderly fashion.
]]>Comment on Bubble Sort in C – [Program & Algorithm] Step-by-Step Explanation by Sian Rees
https://hackr.io/blog/bubble-sort-in-c#comment-770
Wed, 21 Nov 2018 20:41:22 +0000https://hackr.io/blog/?p=730#comment-770There is nothing like the best sorting algorithm. That is depends on the situation and the implementation technique. Furthermore, Merge and heap sort have the best asymptotic runtime complexity because of its O(n log n) complexity in the worst case. After that, bubble and insertion sort comes in the list with O(n^2) complexity.
]]>