#include<stdio.h> #include<conio.h> int main() { char str[100],temp; int i,j; clrscr(); printf("Enter the string :"); gets(str); printf("%s in ascending order is -> ",str); for(i=0;str[i];i++) { for(j=i+1;str[j];j++) { if(str[j]<str[i]) { temp=str[j]; str[j]=str[i]; str[i]=temp; } } } printf("%s\n",str); getch(); return 0; } Output: Enter the string : syntax syntax in ascending order is -> anstxy
The following C function will sort an array of type int using an optimal bubble sort.
void bubble_sort(int* arr, unsigned size)
{
unsigned new_size, index;
if (!arr )
return;
while( size )
{
new_size = 0;
for (index=1; index<size; ++index)
{
if (arr[index]<arr[index-1])
{
int temp = arr[index];
arr[index] = arr[index-1];
arr[index-1] = temp;
new_size = i;
}
}
size = new_size;
}
}
#include <stdio.h>
#include <stdlib.h>
void show_all( int a[], int n ) {
int i;
for( i = 0; i < n; i++ )
printf("%d ", a[i] );
printf("\n",a[i]);
}
void sel_sort( int a[], int n ){
int i, j, m;
for( i = 0; i < n - 1; i++ ){
m = i;
for( j = i + 1; j < n; j++ )
if( a[j] < a[m] )
m = j;
if( i != m )
a[i] ^= a[m] ^= a[i] ^= a[m]; // swap
printf("Sort step %d : ", i+1);
show_all( a, n );
}
}
int main()
{
const int max = 10;
int a[max] = { 2, 3, 9, 6, 8, 1, 5, 7, 4 };
printf( "Unsorted : " );
show_all( a, max );
sel_sort( a, max );
printf( "Sorted : " );
show_all( a, max );
return( 0 );
}
// generic bubble sort for integers
void bubbleSort(int[] ns) {
// minimize calculations during nested loop, calculate second to last
// index here instead of in loop
int lastIndex = ns.length - 1;
// be a "smart" loop: stop going when a whole pass has occurred with no
// changes
int hasChanged;
// keep doing passes until we have no changes
do {
hasChanged = 0;
// single pass of bubble on ns
for (int i = 0; i < lastIndex; ++i) {
// if left number is greater than right number, swap
if (ns[i] > ns[i + 1]) {
int temp = ns[i];
hasChanged = 1;
ns[i] = ns[i + 1];
ns[i + 1] = temp;
}
}
} while (hasChanged == 1);
}
#include<iostream>
#include<iomanip>
#include<time.h>
template<class T>
void exch( T& x, T& y )
{
T tmp=x;
x=y;
y=tmp;
}
template<class T>
void quicksort( T a[], int l, int r )
{
int i;
static int j;
tail_call:
if(r<=l)
return;
// Select random pivot and move it to end of array.
exch( a[get_rand(l,r)], a[r] );
i = l-1;
j = r;
while(1)
{
// Locate the 1st value from left that is not less than the pivot.
while(a[++i]<a[r]);
// Locate the 1st value from right
while(a[r]<a[--j])
if(j==l)
break;
if(i>=j)
break;
exch(a[i], a[j]);
}
exch( a[i], a[r] );
// Determine the smaller subset
static int l1, l2;
l1=l, l2=i-1;
int r1=i+1, r2=r;
if(r2-r1<l2-l1)
{
l1=r1, l2=r2;
r1=l, r2=i-1;
}
// recurse over smaller subset, tail call the other
quicksort( a, l1, l2);
l=r1, r=r2;
goto tail_call;
}
template<class T>
void quicksort( T a[], const size_t size )
{
if( size < 2 )
return;
quicksort( a, 0, size-1 );
}
int main()
{
srand((unsigned)time(NULL));
const size_t size=10;
int a[size];
std::cout<<"Unsorted:";
for(size_t index=0; index<size; ++index)
std::cout<<std::setw(6)<<(a[index]=rand());
std::cout<<endl;
quicksort(a,size);
std::cout<<std::setw(9)<<"Sorted:";
for(size_t index=0; index<size; ++index)
std::cout<<std::setw(6)<<a[index];
std::cout<<endl;
}
/* using ellipses (...) to represent tabs for clarity */
sort (int a[], int N) {
... int swap = 1;
... int i;
... int temp;
... while (swap) {
... ... swap = 0;
... ... for (i=0; i<N; i++) {
... ... ... if (a[i] > a[i+1]) {
... ... ... ... swap = 1;
... ... ... ... temp = a[i];
... ... ... ... a[i] = a[i+1];
... ... ... ... a[i+1] = temp;
... ... ... }
... ... }
... }
}
#include<stdio.h>
#define MAX 100
main()
{
int arr[MAX],i,j,k,temp,n,xchanges;
printf("\n enter the nuber of element :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the element %d: i+1 ");
scanf("%d",&arr[i]);
}
printf("\n Unsoted list is : ") ;
for(i=0;i<n;i++)
printf("%d",arr[i]);
printf("\n");
/* bubble sort*/
for(i=0;i<n-1;i++)
{
xchanges=0;
for(j=0;j<n-1-i;j++);
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
xchanges++;
}
if(xchanges==0)
break;
printf("\n After pass %d elements are",i+1);
for(k=0;k<n;k++)
printf("\n %d",arr[k]);
printf("\n");
}
printf("\n Sorted list is:");
for (i=0;i<n;i++)
printf("\n %d",arr[i]);
printf("\n ");
}
}
***************************************************
The code above is not appropriate..........
check out this blog for an easier solution..happy programming :)
http://kuntalleads.wordpress.com/2013/05/25/bubble-sort/
#include<iostream>
#include<time.h>
int get_rand(int range_min=0, int range_max=RAND_MAX)
{
return((int) ((double)rand() / (RAND_MAX + 1) * (range_max - range_min) + range_min));
}
void bubble_sort(int* arr, int size)
{
if(size)
{
do
{
int n=0;
for(int i=1; i<size; ++i)
{
if(arr[i-1]>arr[i])
{
arr[i-1]^=arr[i]^=arr[i-1]^=arr[i];
n=i;
}
}
size=n;
}
while( size);
}
}
void print_array(int* arr, int size, char* prompt)
{
std::cout<<prompt<<"\t";
for(int i=0; i<size; ++i)
std::cout<<arr[i]<<" ";
std::cout<<std::endl;
}
int main()
{
srand((unsigned) time(NULL));
int size = get_rand(10,20);
int* arr = new int[size];
for(int i=0; i<size; ++i)
arr[i]=get_rand(0,20);
print_array(arr,size,"Before sorting");
bubble_sort(arr, size);
print_array(arr,size,"After sorting");
return(0);
}
In exchange for me writing what is obviously a homework assignment, you get to figure out how this works and how to use it without any comments from me. I suggest that you do that, otherwise you won't learn how to do these kind of things.
sort (int *a, int N) {
int i, swap = 1;
while (swap) {
swap = 0;
for (i=0; i if (a[i] <= a[i+1]) continue; swap = 1; a[i] ^= a[i+1]; a[i+1] ^= a[i]; a[i] ^= a[i+1]; } } } }
Sequential search is the only way to search an unsorted array unless you resort to a multi-threaded parallel search where all threads concurrently search a portion of the array sequentially.
Reference:cprogramming-bd.com/c_page1.aspx# array programming
How to write a program for mouse in microprocessor?
To write a C++ program to display the student details using class and array of object.
int findMax(int *array) { int max = array[0]; for(int i = 1; i < array.length(); i++) { if(array[i] > max) max = array[i] } return max; }
Sequential search is the only way to search an unsorted array unless you resort to a multi-threaded parallel search where all threads concurrently search a portion of the array sequentially.
Reference:cprogramming-bd.com/c_page1.aspx# array programming
How to write a program for mouse in microprocessor?
To write a C++ program to display the student details using class and array of object.
write an assembly language program to find sum of N numbers
array type
int findMax(int *array) { int max = array[0]; for(int i = 1; i < array.length(); i++) { if(array[i] > max) max = array[i] } return max; }
n-1 times
maxValue = function (array) {mxm = array[0];for (i=0; i<array.length; i++) {if (array[i]>mxm) {mxm = array[i];}}return mxm;}; i don't know
public static int[] reverseArray(int[] array) { int i = 0, j = array.length - 1; for (i = 0; i < array.length / 2; i++, j--) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } return array; }
If the array is unsorted, the complexity is O(n) for the worst case. Otherwise O(log n) using binary search.
#include<stdio.h> #include<conio.h> void main() { clrscr(); int n,i,j,temp,a[50]; printf("Enter how many elements="); scanf("%d",&n); printf("Enter %d elements",n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } for(i=1;i<=n-1;i++) { for(j=1;j<=n-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("\nSorted Array:\n"); for(i=1;i<=n;i++) { printf("\n%d",a[i]); } getch(); }