|
@@ -0,0 +1,80 @@
|
|
|
|
|
+#include <stdio.h>
|
|
|
|
|
+#include <stdlib.h>
|
|
|
|
|
+#include <time.h>
|
|
|
|
|
+
|
|
|
|
|
+struct Darray {
|
|
|
|
|
+ int size;
|
|
|
|
|
+ int data[]; //flexible array member
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+//randomized array
|
|
|
|
|
+void rarr(struct Darray *darr) {
|
|
|
|
|
+ for (int i=0; i<darr->size; ++i) {
|
|
|
|
|
+ darr->data[i] = rand() % 32;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (darr == NULL) {
|
|
|
|
|
+ printf("failed");
|
|
|
|
|
+ exit(1);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+struct Darray *createdarray(int mysize){
|
|
|
|
|
+ struct Darray *darrstruct = malloc(mysize * sizeof(int) + sizeof(*darrstruct));
|
|
|
|
|
+
|
|
|
|
|
+ if (darrstruct == NULL) {
|
|
|
|
|
+ printf("failed");
|
|
|
|
|
+ exit(1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ darrstruct->size = mysize;
|
|
|
|
|
+ rarr(darrstruct);
|
|
|
|
|
+
|
|
|
|
|
+ return darrstruct;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void msort(int *myarr, int mysize) {
|
|
|
|
|
+ for (int i = 0 ; i < mysize; i++) {
|
|
|
|
|
+ for (int j = 0 ; j < mysize ; j ++)
|
|
|
|
|
+ if (myarr[i] > myarr[j]) {
|
|
|
|
|
+ int k = myarr[i];
|
|
|
|
|
+ myarr[i] = myarr[j];
|
|
|
|
|
+ myarr[j] = k;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void bsort(struct Darray *myarr) {
|
|
|
|
|
+ for (int i = 0; i < myarr->size; i++) {
|
|
|
|
|
+ for (int j = 0; j < myarr->size -1 ; j++ ) {
|
|
|
|
|
+ if (myarr->data[j] < myarr->data[j+1]) {
|
|
|
|
|
+ int k = myarr->data[j];
|
|
|
|
|
+ myarr->data[j] = myarr->data[j+1];
|
|
|
|
|
+ myarr->data[j+1] = k;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+int main() {
|
|
|
|
|
+ srand(time(NULL));
|
|
|
|
|
+ int mysize = 7;
|
|
|
|
|
+ struct Darray *myarray;
|
|
|
|
|
+ myarray = createdarray(mysize);
|
|
|
|
|
+
|
|
|
|
|
+ printf("myarr:\n");
|
|
|
|
|
+ for (int i = 0 ; i < myarray->size; i++) {
|
|
|
|
|
+ printf("%d\n", myarray->data[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ bsort(myarray);
|
|
|
|
|
+
|
|
|
|
|
+ printf("sorted myarr:\n");
|
|
|
|
|
+ for (int i = 0 ; i < myarray->size; i++) {
|
|
|
|
|
+ printf("%d\n", myarray->data[i]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ free(myarray);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+
|
|
|
|
|
+}
|