Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 5000
- typedef struct{
- char nume[35];
- float nota;
- }student;
- void quicksort(student st[], int s, int d) {
- int i = s;
- int j = d;
- student x = st[(s + d) / 2];
- do{
- while(strcmp(st[i].nume, x.nume) < 0) i++;
- while(strcmp(st[j].nume, x.nume) > 0) j--;
- if(i <= j) {
- student temp = st[i];
- st[i] = st[j];
- st[j] = temp;
- i++;
- j--;
- }
- }
- while(i <= j);
- if(s < j) quicksort(st, s, j);
- if(d > i) quicksort(st, i ,d);
- }
- void printStructure(student st[], int n) {
- for(int i = 0; i < n; i++) {
- printf("%s\t%f", st[i].nume, st[i].nota);
- printf("\n");
- }
- }
- int main() {
- student s[MAX];
- FILE *f = fopen("Studenti2.txt", "r");
- if(f == NULL) {
- printf("Eroare la deschidere. ");
- exit(1);
- }
- for(int i = 0; i < MAX; i++) {
- fgets(s[i].nume,35,f);
- s[i].nume[strlen(s[i].nume) - 1] = '\0';
- fscanf(f, "%f\n", &s[i].nota);
- }
- printStructure(s, 100);
- printf("\nSorted: \n");
- quicksort(s, 0, MAX - 1);
- printStructure(s, 50);
- fclose(f);
- return 0;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 5000
- typedef struct oameni{
- char nume[35];
- float nota;
- }student;
- /*void heapify(int *s, student *temp, int *d) {
- int i, j;
- int ret = 0;
- i = *s;
- j = 2 * i;
- *temp = s[i];
- ret = 0;
- while((j <= *d) && (!ret)) {
- if(j < *d)
- if(strcmp(st[j].nume, st[j+1].nume) < 0) j += 1;
- if(strcmp((*temp).nume,st[j].nume) < 0) {
- st[i] = st[j];
- i = j;
- j = 2 * i;
- } else {
- ret = 1;
- }
- st[i] = temp;
- }
- }*/
- /*void heapsort(student st[], int n) {
- int s, d;
- student temp;
- s = (n / 2) + 1; d = n - 1;
- while(s > 0) {
- s = s - 1;
- heapify(&s, &temp, &d);
- }
- while(d > 0) {
- temp = st[0];
- st[0] = st[d];
- st[d] = temp;
- }
- }*/
- void heapSort(student st[]) {
- buildMaxHeap(st);
- for(int i = n; i >= 1; i--) {
- student temp = st[1];
- st[1] = st[i];
- st[i] = st[1];
- n -= 1;
- heapify(st, i);
- }
- }
- void heapify(student st[], int i) {
- int left = 2*i;
- int right = 2*i+1;
- }
- void printStructure(student st[], int n) {
- for(int i = 0; i < n; i++) {
- printf("%s\t%f", st[i].nume, st[i].nota);
- printf("\n");
- }
- }
- int main() {
- student s[MAX];
- FILE *f = fopen("Studenti2.txt", "r");
- if(f == NULL) {
- printf("Eroare la deschidere. ");
- exit(1);
- }
- for(int i = 0; i < MAX; i++) {
- fgets(s[i].nume,35,f);
- s[i].nume[strlen(s[i].nume) - 1] = '\0';
- fscanf(f, "%f\n", &s[i].nota);
- }
- heapsort(s, MAX);
- printStructure(s, MAX);
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement