CS1132_Fall_2011_Lecture10_BB

CS1132_Fall_2011_Lecture10_BB - Lecture 10 Chapter 13...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 10 Chapter 13 Sorting MIDTERM2 date 11/17/11 CS 103 2 Selection Sort Array void isort(float arr, int n); int fmin(float arr, int b, int n); int main(void) { float arr1[5] = {4.3, 6.7, 2.8, 8.9, 1.0}; int i = 0; printf("\nSelection Sort for the following array: "); printf("\n---------------------------------------\n"); for(i = 0; i < 5; i++) printf("%.2f\t", arr1[i]); printf("\n---------------------------------------\n"); ssort(arr1,5); return 0; } 3 void ssort(float arr, int n int i, s, w; float smin; for(s = 0; s < n - 1; s++) { w = fmin(arr, s, n); smin = arr[w]; arr[w] = arr[s]; arr[s] = smin; for(i = 0; i < 5; i++) printf("%.2f\t", arr[i]); printf("\n"); } } int fmin(float arr, int b, int n) { int f = b; int i; for(i = b + 1; i < n; i++) if(arr[i] < arr[f]) f = i; return f;} Selection Sort Array Output $ ./a.out Insertion Sort for the following array:--------------------------------------- 4.30 6.70 2.80 8.90 1.00--------------------------------------- 1.00 6.70 2.80 8.90 4.30 1.00 2.80 6.70 8.90 4.30 1.00 2.80 4.30 8.90 6.70 1.00 2.80 4.30 6.70 8.90 4 Selection Sort Array #include <stdio.h> void selectionsort(int a, int size); int main(void) { int arr[10] = {10, 21, 4, 1, 18, 7,15,5,9,2}; int k; printf("before sort:\n"); for(k = 0; k < 10; k++) printf("%d ", arr[k]); printf("\n"); selectionsort(arr,10); printf("at the end of sort:\n"); for(k = 0; k < 10; k++) printf("%d ", arr[k]); printf("\n"); return 0; } 5 void selectionsort(int arr, int siz int position,temp, i, j, k ; for ( i = 0 ; i <10 ; ++ i) { position = i ; for(j = 10-1; j > i; --j ) { if ( arr[j] < arr[position]) po } temp=arr[i]; arr[i] =arr[position]; arr[position] = temp; printf("end loop %d \n ",j); for(k = 0; k < 10; k++) printf(" % printf("\n"); } } Selection Sort ArrayOutput before sort: 10 21 4 1 18 7 15 5 9 2 end loop 0 1 21 4 10 18 7 15 5 9 2 end loop 1 1 2 4 10 18 7 15 5 9 21 end loop 2 1 2 4 10 18 7 15 5 9 21 end loop 3 1 2 4 5 18 7 15 10 9 21 end loop 4 1 2 4 5 7 18 15 10 9 21 end loop 5 1 2 4 5 7 9 15 10 18 21 end loop 6 1 2 4 5 7 9 10 15 18 21 end loop 7 1 2 4 5 7 9 10 15 18 21 end loop 8 1 2 4 5 7 9 10 15 18 21 end loop 9 1 2 4 5 7 9 10 15 18 21 at the end of sort: 1 2 4 5 7 9 10 15 18 21 6 Insertion Sort Array #include <stdio.h> void insertionsort(int a, int size); int main(void) { int arr[10] = {10, 21, 4, 1, 18, 7,15,5,9,2}; int k; printf("before sort:\n"); for(k = 0; k < 10; k++) printf("%d ", arr[k]); printf("\n"); insertionsort(arr,10); printf("at the end of sort:\n"); for(k = 0; k < 10; k++) printf("%d ", arr[k]); printf("\n"); return 0; } 7 void insertionsort(int arr, int siz int i,j,k, temp; for ( j = 1; j <= 9; j++) { temp = arr[j]; or (i = j - 1; i >= 0 && temp < a arr[i + 1] = arr[i]; arr[i]= temp; } printf("end loop %d \n ",j); for(k = 0; k < 10; k++) printf(" % printf("\n"); } } Insertion Sort ArrayOutput before sort: 10 21 4 1 18 7 15 5 9 2 end loop 1 10 21 4 1 18 7 15 5 9 2 end loop 2 4 10 21 1 18 7 15 5 9 2 end loop 3 1 4 10...
View Full Document

This note was uploaded on 11/14/2011 for the course CSCI 1132 taught by Professor Haya during the Fall '11 term at GWU.

Page1 / 53

CS1132_Fall_2011_Lecture10_BB - Lecture 10 Chapter 13...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online