(Lecture March 9 2012) |
|||
Line 1: | Line 1: | ||
− | //Peachanok Lertkajornkitti ECE 264 | + | [[Category:ECE264]] |
+ | [[Category:sorting]] | ||
+ | [[Category:programming]] | ||
+ | [[Category:C]] | ||
+ | |||
+ | //Peachanok Lertkajornkitti [[ECE264|ECE 264]] | ||
+ | |||
//Lecture Fri 9Mar | //Lecture Fri 9Mar | ||
+ | |||
//Lecture 17 | //Lecture 17 | ||
Line 118: | Line 125: | ||
} | } | ||
} | } | ||
+ | ---- | ||
+ | [[2012_Spring_ECE_264_Lu|Back to ECE 264 Spring 2012]] |
Latest revision as of 07:58, 9 March 2012
//Peachanok Lertkajornkitti ECE 264
//Lecture Fri 9Mar
//Lecture 17
/*Stats on the exam 1 mean: 7.10/14
Q1: 2.49/5 (Outcome 1,FILE, passed if >= 2.5) Q2: 3.10/6 (OUtcome 2,STRUCT, passed if >= 2.5) Q3: 1.35/3
Next exam test OUTCOME 3 THIRD EXAM test OUTCOME 1&2 (Make up exam)
SORTING -Insertion sort -Bubble sort -Selection sort
-Merge sort -Quick sort
SELECTION SORT -find min value -swap with first position -repeats until end of array (advance position every time)
i.e 42| 19 73 32 4
4 19 73 32 42 (Swap 4 and 42)
4 19 73 32 42 (Looking at values to the right of 19, 19 is the smallest)
4 19 32 73 42 (Looking at the values to the right of 73, 32 is the smallest, swap 32 with 73)
4 19 32 42 73 (Looking at the values to the right of 73, 42 is the smallest, swap)
- /
//EXERCISE 5
- include<stdio.h>
- include<stdlib.h>
typedef struct{ int length; int *data; }IntArray
IntArray *IntArray_create //incomplete but similar to Maze_create
IntArray IntArray_destroy //remember to destroy every time you create
IntArray *IntArray_load(char *file) { FILE *f = fopen(argv[1],"r"); if(f == NULL) { return NULL; }
const int BUFFER_LENGTH = 80; char buffer[BUFFER_LENGTH); int lineNumber = 0; while(fgets(buffer,BUFFER_LENGTH,f) != NULL){ array->data[lineNumber++] = strtol(buffer,NULL,10);
//or fscanf(f, "%d", array->data[lineNumber++]; }
fclose(f); return array; }
int main(int argc, char *argv[]) { if(argc != 2){ return EXIT_FAILURE; }
intArray *array = IntArray_load(argv[1]); if(array == NULL){ return EXIT_FAILURE;
//Sort IntArray_sort(array);
}
int IntArray_findMin(IntArray *array,int offset) { int i; int min = array->data[offset]; int minIndex; for (i = offset;1<array->length;i++){ if(array->data[i] < min) { min = array->data[i]; minIndex = i; } } return minIndex; }
void IntArray_swap(IntArray *array, int a,int b) { int tmp = array->data[a]; array->data[a] = array->data[b]; array->data[b] = tmp; } //SORTING void IntArray_sort(IntArray *array) { int i; for(i=0;i<array->length - 1; i++){ int minIndex = IntArray_findMin(array,i); IntArray_swap(array,i,minIndex); } }