×

# include<stdio.h>

int main() { int t,i,j,temp=0; scanf("%d",&t); int a[t]; for(i=0;i<t;i++) {="" scanf("%d",&a[i]);="" }="" for(i="0;i&lt;t;i++)" {="" for(j="0;j&lt;t-1;j++)" {="" if(a[j]="">a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } }

}
for(i=0;i<t;i++)
{
printf("%d\n",a[i]);
}
return 0;


}

131222
accept rate: 0%

 3 You are using bubble sort. It has a time complexity of O(n^2) It won't work because of the time limit. Try to use a O(n log(n)) complexity algorithm like merge sort. Here's my solution using merge sort, http://www.codechef.com/viewsolution/3306706 There are predefined functions in almost all the languages that perform sorting in O(n log(n)). For example, sort() function in C++ (Header: < algorithm >) I would suggest that you also use these functions for the sake of knowledge. But it is very important that you understand merge sort, quick sort and many more sorting algorithms and their complexities. Knowledge of sorting algorithms is important. Here are a few sorting algorithms: 1. Merge sort 2. Quick sort 3. Insertion sort. 4. Selection sort 5. Bubble sort And many more answered 30 Jun '14, 23:15 1.8k●4●9●33 accept rate: 14%
 1 Adding to @pratku123 answer's, Here is a nice website : http://www.sorting-algorithms.com/ to fundamentally understand how different sorting algorithms works and it clears show no algorithm is best. In some cases insertion sort is more optimal than merge sort, and in some scenarios, though other sorts may take more time, but merge sort is avoided due to overhead and memory needs. answered 01 Jul '14, 00:35 151●1●3●7 accept rate: 20%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×205
×40
×38

question asked: 30 Jun '14, 23:06

question was seen: 2,326 times

last updated: 01 Jul '14, 00:35