-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e5dad3f
commit f90e0a0
Showing
1 changed file
with
36 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,44 @@ | ||
/** | ||
* @file | ||
* @brief Sorting of array list using [bead | ||
* sort](https://en.wikipedia.org/wiki/Bead_sort) | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
/** | ||
* @addtogroup sorting Sorting algorithms | ||
* @{ | ||
*/ | ||
/** Create easy access of elements from a 2D matrix stored in memory as a 1D | ||
* array | ||
*/ | ||
#define BEAD(i, j) beads[i * max + j] | ||
|
||
/** | ||
* Displays the array, passed to this method | ||
* @param [in] arr array to display | ||
* @param [in] n number of elements in the array | ||
*/ | ||
void display(const int *arr, int n) | ||
// Function to display the array | ||
void displayArray(int arr[], int n) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
printf("Array elements: "); | ||
for (int i = 0; i < n; i++) | ||
{ | ||
printf("%d ", arr[i]); | ||
} | ||
|
||
printf("\n"); | ||
} | ||
|
||
/** This is where the sorting of the array takes place | ||
* @param [in,out] a array to be sorted | ||
* @param [in] len Array Size | ||
*/ | ||
void bead_sort(int *a, size_t len) | ||
{ | ||
int i, j, max, sum; | ||
unsigned char *beads; | ||
|
||
for (i = 1, max = a[0]; i < len; i++) | ||
if (a[i] > max) | ||
max = a[i]; | ||
|
||
beads = calloc(1, max * len); | ||
|
||
/* mark the beads */ | ||
for (i = 0; i < len; i++) | ||
for (j = 0; j < a[i]; j++) BEAD(i, j) = 1; | ||
|
||
for (j = 0; j < max; j++) | ||
{ | ||
/* count how many beads are on each post */ | ||
for (sum = i = 0; i < len; i++) | ||
{ | ||
sum += BEAD(i, j); | ||
BEAD(i, j) = 0; | ||
} | ||
/* mark bottom sum beads */ | ||
for (i = len - sum; i < len; i++) BEAD(i, j) = 1; | ||
} | ||
|
||
for (i = 0; i < len; i++) | ||
{ | ||
for (j = 0; j < max && BEAD(i, j); j++) | ||
; | ||
a[i] = j; | ||
} | ||
free(beads); | ||
} | ||
/** @} */ | ||
|
||
/** Main function */ | ||
int main(int argc, const char *argv[]) | ||
{ | ||
int n; | ||
printf("Enter size of array:\n"); | ||
scanf("%d", &n); // E.g. 8 1 2 3 | ||
|
||
printf("Enter the elements of the array\n"); | ||
int i; | ||
int *arr = (int *)malloc(n * sizeof(int)); | ||
for (i = 0; i < n; i++) | ||
{ | ||
scanf("%d", &arr[i]); | ||
} | ||
|
||
printf("Original array: "); | ||
display(arr, n); | ||
|
||
bead_sort(arr, n); | ||
|
||
printf("Sorted array: "); | ||
display(arr, n); | ||
|
||
free(arr); | ||
return 0; | ||
} | ||
int fibMonaccianSearch(int arr[], int x, int n) | ||
{ | ||
/* Initialize fibonacci numbers */ | ||
int fibMMm2 = 0; // (m-2)'th Fibonacci No. | ||
int fibMMm1 = 1; // (m-1)'th Fibonacci No. | ||
int fibM = fibMMm2 + fibMMm1; // m'th Fibonacci | ||
|
||
/* fibM is going to store the smallest Fibonacci | ||
Number greater than or equal to n */ | ||
while (fibM < n) | ||
{ | ||
fibMMm2 = fibMMm1; | ||
fibMMm1 = fibM; | ||
fibM = fibMMm2 + fibMMm1; | ||
} | ||
|
||
// Marks the eliminated range from front | ||
int offset = -1; | ||
|
||
/* while there are elements to be inspected. Note that | ||
we compare arr[fibMm2] with x. When fibM becomes 1, | ||
fibMm2 becomes 0 */ | ||
while (fibM > 1) | ||
{ | ||
// Check if fibMm2 is a valid location | ||
|
||
// sets i to the min. of (offset+fibMMm2) and (n-1) | ||
int i = ((offset + fibMMm2) < (n - 1)) ? (offset + fibMMm2) : (n - 1); | ||
|
||
/* If |