CS1132_Fall_2011_Lecture10_BB

# CS1132_Fall_2011_Lecture10_BB - Lecture 10 Chapter 13...

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

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
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(&amp;quot;\nSelection Sort for the following array: &amp;quot;); printf(&amp;quot;\n---------------------------------------\n&amp;quot;); for(i = 0; i &amp;lt; 5; i++) printf(&amp;quot;%.2f\t&amp;quot;, arr1[i]); printf(&amp;quot;\n---------------------------------------\n&amp;quot;); ssort(arr1,5); return 0; } 3 void ssort(float arr, int n int i, s, w; float smin; for(s = 0; s &amp;lt; n - 1; s++) { w = fmin(arr, s, n); smin = arr[w]; arr[w] = arr[s]; arr[s] = smin; for(i = 0; i &amp;lt; 5; i++) printf(&amp;quot;%.2f\t&amp;quot;, arr[i]); printf(&amp;quot;\n&amp;quot;); } } int fmin(float arr, int b, int n) { int f = b; int i; for(i = b + 1; i &amp;lt; n; i++) if(arr[i] &amp;lt; 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 &amp;lt;stdio.h&amp;gt; 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(&amp;quot;before sort:\n&amp;quot;); for(k = 0; k &amp;lt; 10; k++) printf(&amp;quot;%d &amp;quot;, arr[k]); printf(&amp;quot;\n&amp;quot;); selectionsort(arr,10); printf(&amp;quot;at the end of sort:\n&amp;quot;); for(k = 0; k &amp;lt; 10; k++) printf(&amp;quot;%d &amp;quot;, arr[k]); printf(&amp;quot;\n&amp;quot;); return 0; } 5 void selectionsort(int arr, int siz int position,temp, i, j, k ; for ( i = 0 ; i &amp;lt;10 ; ++ i) { position = i ; for(j = 10-1; j &amp;gt; i; --j ) { if ( arr[j] &amp;lt; arr[position]) po } temp=arr[i]; arr[i] =arr[position]; arr[position] = temp; printf(&amp;quot;end loop %d \n &amp;quot;,j); for(k = 0; k &amp;lt; 10; k++) printf(&amp;quot; % printf(&amp;quot;\n&amp;quot;); } } 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 &amp;lt;stdio.h&amp;gt; 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(&amp;quot;before sort:\n&amp;quot;); for(k = 0; k &amp;lt; 10; k++) printf(&amp;quot;%d &amp;quot;, arr[k]); printf(&amp;quot;\n&amp;quot;); insertionsort(arr,10); printf(&amp;quot;at the end of sort:\n&amp;quot;); for(k = 0; k &amp;lt; 10; k++) printf(&amp;quot;%d &amp;quot;, arr[k]); printf(&amp;quot;\n&amp;quot;); return 0; } 7 void insertionsort(int arr, int siz int i,j,k, temp; for ( j = 1; j &amp;lt;= 9; j++) { temp = arr[j]; or (i = j - 1; i &amp;gt;= 0 &amp;amp;&amp;amp; temp &amp;lt; a arr[i + 1] = arr[i]; arr[i]= temp; } printf(&amp;quot;end loop %d \n &amp;quot;,j); for(k = 0; k &amp;lt; 10; k++) printf(&amp;quot; % printf(&amp;quot;\n&amp;quot;); } } 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
Ask a homework question - tutors are online