Skip to content

Commit

Permalink
shell_sort.cpp Adds Shell Sort Algorithm
Browse files Browse the repository at this point in the history
Closes #270
  • Loading branch information
joao-p-marques committed Oct 8, 2018
1 parent b993b87 commit eeb228c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions shell_sort/Cpp/shell_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <iostream>

using std::cout;

void insertion_sort(int a[], int a_size) {
int i = 0;
while (i < a_size) {
int j = i;
while (j > 0 && a[j-1] > a[j]) {
// swap
int temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
j--;
}
i++;
}

cout << "After insertion sort: ";
for (int i = 0; i < a_size; i++) {
cout << a[i] << " ";
}
cout << std:: endl;
}

void shell_sort(int a[], int a_size) {
int gap = 1;
while (gap < a_size/3) {
gap = gap * 3 + 1; //calcutale biggest gap to start with.
}

while (gap > 0) {
for (int i = 0; i < a_size - gap; i++) {
if (a[i] > a[i + gap]) {
// swap
int temp = a[i];
a[i] = a[i + gap];
a[i + gap] = temp;
}
}
gap = (gap - 1) / 3;
}

cout << "After shell sort: ";
for (int i = 0; i < a_size; i++) {
cout << a[i] << " ";
}
cout << std:: endl;

insertion_sort(a, a_size);
}

int main(void) {
int a[8] = {3, 7, 4, 9, 5, 2, 6, 1};
int a_size = sizeof(a) / sizeof(a[0]);

shell_sort(a, a_size);

for (int i = 0; i < a_size; i++) {
cout << a[i] << " ";
}
}

1 comment on commit eeb228c

@sangamcse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on eeb228c.

There are 71 results for the section all.cpp. They have been shortened and will not be shown inline because they are more than 10.

Message File Line
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 8
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 9
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 11
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 12
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 13
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 14
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 15
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 16
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 17
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 18
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 19
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 20
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 21
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 22
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 23
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 24
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 26
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 27
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 28
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 29
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 34
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 35
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 37
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 39
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 40
Line contains following spacing inconsistencies: - Spaces used instead of tabs. quicksort/Cpp/quick_sort.cpp 41
Line contains following spacing inconsistencies: - No newline at EOF. quicksort/Cpp/quick_sort.cpp 42
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 6
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 7
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 8
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 9
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 10
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 11
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 12
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 13
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 14
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 15
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 16
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 17
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 19
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 20
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 21
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 22
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 23
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 27
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 28
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 29
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 30
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 32
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 33
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 34
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 35
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 36
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 37
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 38
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 39
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 40
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 41
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 42
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 44
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 45
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 46
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 47
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 48
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 50
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 54
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 55
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 57
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 59
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 60
Line contains following spacing inconsistencies: - Spaces used instead of tabs. shell_sort/Cpp/shell_sort.cpp 61

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

Please sign in to comment.