File tree Expand file tree Collapse file tree 4 files changed +7
-5
lines changed Expand file tree Collapse file tree 4 files changed +7
-5
lines changed Original file line number Diff line number Diff line change @@ -59,7 +59,7 @@ From [Wikipedia][quick-wiki]: Quicksort (sometimes called partition-exchange sor
59
59
__ Properties__
60
60
* Worst case performance O(n^2)
61
61
* Best case performance O(n log n) or O(n) with three-way partition
62
- * Average case performance O(n^2 )
62
+ * Average case performance O(n log n )
63
63
64
64
###### View the algorithm in [ action] [ quick-toptal ]
65
65
Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ int main() {
21
21
top = n_terms ;
22
22
23
23
do {
24
- mid = ( bottom + top ) / 2 ;
24
+ mid = bottom + ( top - bottom )/ 2 ; // same as (bottom + top / 2) but considers overflow condition when bottom + top might be larger than int
25
25
if (item < arr [mid ])
26
26
top = mid - 1 ; // Here we are dividing the array into two equal parts
27
27
else if (item > arr [mid ]) /* if target element > mid part of array , we do the search in the upper part of the array
Original file line number Diff line number Diff line change 3
3
4
4
#include <stdio.h>
5
5
6
+ void swap (int * ,int * );
6
7
7
8
void bubblesort (int arr [], int size )
8
9
{
9
10
int i , j ;
10
- for (i = 0 ; i < size ; i ++ ) // Function where the actual algorithm is implemented
11
+ for (i = 0 ; i < size - 1 ; i ++ ) // Function where the actual algorithm is implemented
11
12
{
12
- for (j = 0 ; j < size - i ; j ++ )
13
+ for (j = 0 ; j < size - i - 1 ; j ++ )
13
14
{
14
15
if (arr [j ] > arr [j + 1 ])
15
16
swap (& arr [j ], & arr [j + 1 ]);
@@ -39,6 +40,7 @@ int main()
39
40
40
41
for (i = 0 ; i < size ; i ++ )
41
42
printf (" %d " , array [i ]);
43
+ printf (" " );
42
44
return 0 ;
43
45
44
46
}
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ int main(){
16
16
for (i = 1 ;i < s ;i ++ ){
17
17
temp = a [i ];
18
18
j = i - 1 ;
19
- while ((temp < a [ j ]) && ( j >= 0 )){
19
+ while ((j >= 0 ) && ( temp < a [ j ] )){
20
20
a [j + 1 ]= a [j ];
21
21
j = j - 1 ;
22
22
}
You can’t perform that action at this time.
0 commit comments