Posts

Showing posts with the label sorting

Bubble Sort In Python

Image
This is a simple program which demonstrates the implementation of the bubble sort algorithm in python. The complexity of the program is O(n 2 ) . The Input is taken from the console and then appended to a list, which is further processed . Output Screen: Program : print 'Enter the list of elements followed by space:' x=raw_input(); x=map(int,x.split()) for i in range(0, len(x)-1): for j in range(0,len(x)-i-1): if(x[j]>x[j+1]): swp=x[j] x[j]=x[j+1] x[j+1]=swp print x Found suggestions, Please comment them here !

Selection Sort Using C

Image
                   Hello guys, selection sort is one of the basic algorithm and programming concept which we need from the very beginning to lifetime of programming. In most of the cases in programming, we need sorted input for the execution of the code in proper manner. But its not a logical habit to ask the use to enter in the sorted manner. So this is one of the basic sorting concept one should know. Description:                      Well, in this concept, the sorting technique lies on a concept of comparing again and again the elements of array which is taken. This simply shows that we have to use looping concept. Here easiest way is to use for  loop. We have to take two for loops, one for selection of one element from array and another one for the rest of the elements which are to be compared. The format of the for loop will be like this, for( i=0; i<n-1; i+...

Selection Sort Using Java

Image
This is a simple program to illustrate how to use selection sort for sorting integers using java code. Selection sort is an in-place sorting technique with time complexity of O(n2). Its time complexity remains same in all the 3 cases (i.e. best, average and worst) .This type of sorting is preferred where memory is limited. Screenshots: Program: import java.util.*; class Sorting { public static void selectionSort(int arr[], int size) { int i, j, pos=0,temp=0; for(i=0;i<size-1;i++) { pos=i; for (j = i+1; j < size; j++) { if (arr[j] < arr[pos]) pos = j; } // Swapping the elements temp = arr[i]; arr[i] = arr[pos]; arr[pos]= temp; } } public sta...

Insertion Sort in c

Image
Insertion sort  is a simple sorting algorithm: a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quick sort, heap sort, or merge sort. However, insertion sort provides several advantages: simple implementation efficient on small data sets uses a fixed amount of memory when running Insertion sort requires the use of two arrays, one ordered, and one unordered. Each repetition of the algorithm moves an item from the unordered list, into a sorted position in the ordered list, until there are no elements left in the unordered list. Sorting is typically done in-place without needing extra memory. The resulting array after  k  iterations has the property where the first  k  + 1 entries are sorted. In each iteration the first remaining entry of the input is removed, inserted into the result at the correct position, thus extending the res...

Bubble Sort

The below program takes an array as an input and prints the elements of the array in sorted order. Adjacent elements are checked in this sorting method. The program is implemented using c. #include<stdio.h> int main() { int temp,i,n,j; printf("\nEnter number of elements that are to be sorted : "); scanf("%d",&n); int a[n]; for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=n-1;i>=0;i--) for(j=1;j<=i;j++) if(a[j-1]>a[j]) { temp=a[j-1]; a[j-1]=a[j]; a[j]=temp; } printf("The elements in sorted order are :"); for(i=0;i<n;i++) printf("%d ",a[i]); } Description: The above program sorts the given array and prints it. The adjacent elements are checked whether the two elements are in sorted order or not. If the two elements are not in sorted order swapping takes place to make them sorted. Output: Case 1...