tag:blogger.com,1999:blog-8125359017846514862023-06-20T06:19:44.044-07:00Algorithm n Algorithmsরেজওয়ানুল হকhttp://www.blogger.com/profile/14861213171354105332noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-812535901784651486.post-65503139729640539532008-07-16T08:34:00.000-07:002008-07-16T09:58:07.158-07:00Sorting Algorithms in C<span style="font-size:100%;">Here we will give you the implementation of different sorting algorithms in C language. All the implementations have been tested using "gcc" compiler in cygwin. We have used file input and output.<br /><br />Input specification: Input file contains 15,000 integers for each case. The numbers are generated in the input file randomly.<br />Output specification: Output file contains the numbers in ascending order.<br /><br /><span style="font-weight: bold;">1. Bubble Sort:</span><br /><br /><span style="font-family: courier new;">#include<stdio.h></span><br /><span style="font-family: courier new;">#include<stdlib.h></span><br /><span style="font-family: courier new;">#define SIZE 15000</span><br /><br /><span style="font-family: courier new;">void bubbleSort(unsigned int[]);</span><br /><br /><span style="font-family: courier new;">int main(void)</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> unsigned int A[SIZE];</span><br /><span style="font-family: courier new;"> int i,j;</span><br /><span style="font-family: courier new;"> if(freopen("bubble.in","r",stdin)==NULL)</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> printf("Error Reading File");</span><br /><span style="font-family: courier new;"> exit(1);</span><br /><span style="font-family: courier new;"> }</span><br /><span style="font-family: courier new;"> freopen("bubble.out","w",stdout);</span><br /><span style="font-family: courier new;"> for(i=0;i<SIZE;i++)</span><br /><span style="font-family: courier new;"> scanf("%d",&A[i]);</span><br /><span style="font-family: courier new;"> bubbleSort(A);</span><br /><span style="font-family: courier new;"> for(i=0;i<SIZE;i++)</span><br /><span style="font-family: courier new;"> printf("%d ",A[i]);</span><br /><span style="font-family: courier new;"> fclose(stdin);</span><br /><span style="font-family: courier new;"> fclose(stdout);</span><br /><span style="font-family: courier new;"> return 0;</span><br /><span style="font-family: courier new;"> }</span><br /><br /><span style="font-family: courier new;">void bubbleSort(unsigned int A[])</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> unsigned int temp;</span><br /><span style="font-family: courier new;"> int i,j;</span><br /><span style="font-family: courier new;"> for(i=0;i<SIZE;i++)</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> for(j=SIZE-1;j>i;j--)</span><br /><span style="font-family: courier new;"> if(A[j]<A[j-1])</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> temp=A[j];</span><br /><span style="font-family: courier new;"> A[j]=A[j-1];</span><br /><span style="font-family: courier new;"> A[j-1]=temp;</span><br /><span style="font-family: courier new;"> }</span><br /><span style="font-family: courier new;"> }</span><br /><span style="font-family: courier new;"> }</span><br /><br /></span><span style="font-size:100%;">Input file: bubble.in<br />Output file: bubble.out<br /></span><span style="font-size:100%;">Complexity: O(n^2)<br /></span><span style="font-size:100%;"><br /></span><span style="font-size:100%;"><span style="font-weight: bold;">2. Insertion Sort<br /><br /></span><span style="font-family: courier new;">#include<stdio.h></span><br /></span><span style="font-family: courier new;font-size:100%;" >#include<stdlib.h><br />#define SIZE 15000<br /><br /></span><span style="font-family: courier new;font-size:100%;" >void insertionSort(unsigned int[]);<br /></span><span style="font-family: courier new;font-size:100%;" ><br />int main(void)<br />{<br />unsigned int A[SIZE];<br />int i,j;<br />if(freopen("insertion.in","r",stdin)==NULL)<br /> {<br /> printf("Error Reading File");<br /> exit(1);<br /> }<br />freopen("insertion.out","w",stdout);<br />for(i=0;i< SIZE;i++)<br /> scanf("%d",&A[i]);<br />insertionSort(A);<br />for(i=0;i< SIZE;i++)<br /> printf("%d ",A[i]);<br />fclose(stdin);<br />fclose(stdout);<br />return 0;<br />}<br /><br /></span><span style="font-size:100%;"><span style="font-family: courier new;">void insertionSort(unsigned int a[])</span><br /><span style="font-family: courier new;">{</span><br /><span style="font-family: courier new;">int i,j;</span><br /><span style="font-family: courier new;">unsigned int key;</span><br /><span style="font-family: courier new;">for(j=1;j< SIZE;j++)</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> key = a[j];</span><br /><span style="font-family: courier new;"> i = j-1;</span><br /><span style="font-family: courier new;"> while(i>=0 && a[i]>key)</span><br /><span style="font-family: courier new;"> {</span><br /><span style="font-family: courier new;"> a[i+1]=a[i];</span><br /><span style="font-family: courier new;"> i=i-1;</span><br /><span style="font-family: courier new;"> }</span><br /><span style="font-family: courier new;"> a[i+1]=key;</span><br /><span style="font-family: courier new;"> }</span><br /><span style="font-family: courier new;">}</span><br /><br /></span><span style="font-size:100%;">Input file: insertion.in<br />Output file: insertion.out<br /></span><span style="font-size:100%;">Complexity: O(n^2)<br /><br />More sorting algorithms to follow..<br /></span><span style="font-size:100%;"><br /><br /></span>রেজওয়ানুল হকhttp://www.blogger.com/profile/14861213171354105332noreply@blogger.com2tag:blogger.com,1999:blog-812535901784651486.post-31125384356291830472008-07-14T22:02:00.000-07:002008-07-14T22:05:33.620-07:00AlgorithmsIn this blog, I will try to give you the simple implementation of different algorithms in C/C++, Java, Python and Haskell. I hope that you will contribute here substantially so that this blog can be very useful for students of computer science.<br /><br />Thanks,<br />Rezwanরেজওয়ানুল হকhttp://www.blogger.com/profile/14861213171354105332noreply@blogger.com1