Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- struct lista{
- struct lista *next;
- float key;
- };
- int dlugosc_T(float * A);
- int dlugosc_L(struct lista * head);
- void lista_dodaj(struct lista * head, struct lista * nowy);
- struct lista * sortwstaw(struct lista * head);
- float * sort_kubelkowe(float * A);
- int main(){
- int i;
- float tablica[] = {0.23, 0.12, 0.56, 0.97, 0.91, 0.54, 0, 0.34, 0.35, -1};
- float * tab = tablica;
- tab = sort_kubelkowe(tablica);
- for(i = 0; i < 9 ; i++){
- printf("%f\n", tab[i]);
- }
- getchar();
- getchar();
- return 1;
- }
- int dlugosc_T(float * A){
- int i;
- for(i = 0; ; i++){
- if(A[i] == -1) break;
- }
- return i + 1;
- }
- int dlugosc_L(struct lista * head){
- int i = 0;
- struct lista * x = head;
- while(x != NULL){
- i++;
- x = x -> next;
- }
- return i;
- }
- void lista_dodaj(struct lista * head, struct lista * nowy){
- struct lista * x = head;
- struct lista * n = calloc(1, sizeof(struct lista));
- n -> key = nowy -> key;
- if(head == NULL){
- head = n;
- return;
- }
- while(x != NULL || x -> next != NULL)
- x = x -> next;
- x -> next = n;
- }
- struct lista * sortwstaw(struct lista * head){
- int i = 0, j;
- int n = dlugosc_L(head);
- struct lista * x = head -> next, *y = NULL, *z = NULL ;
- struct lista * nowa = calloc(1, sizeof(struct lista));
- lista_dodaj(nowa, head);
- y = nowa; z = nowa;
- while(x != NULL){
- if(y -> key < x -> key && (y -> next -> key > x -> key || y -> next == NULL)){
- x -> next = y -> next;
- y -> next = x;
- y = z;
- }
- else if(y -> key > x -> key){
- x -> next = y;
- y = x;
- z = y;
- }
- x = x -> next;
- }
- return nowa;
- }
- float * sort_kubelkowe(float * A){
- int n = dlugosc_T(A);
- int i;
- float * W = calloc(n, sizeof(float));
- struct lista ** B = calloc(10, sizeof(struct lista *));
- struct lista * C = B;
- struct lista * M = calloc(1, sizeof(struct lista *));
- for(i = 0; i < n; i++){
- M -> key = A[i];
- n = floor(10 * A[i]);
- lista_dodaj(B[n], M);
- }
- for(i = 0; i< n - 1; i++){
- B[i] = sortwstaw(B[i]);
- }
- for(i = 0; i < 10; i++){
- while(B[i] != NULL){
- W[i] += B[i] -> key;
- B[i] = B[i] -> next;
- }
- }
- return W;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement