Shortest the program as possible but output will not change
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX_BUF 50 // Size of the buffer to hold intermediate result
#define CUTOFF 18 // Quick sort cutoff
typedef int (*Comparable) ( const void *a, const void *b ); //pointer to function
void low_level_copy(void *target, void *source, unsigned int size){
unsigned int count = 0;
void *from = source;
void *to = target;
if(size < 0)
return;
else if(size == 0)
return;
while(1){
if(count > size)
break;
else if(count == size)
break;
memcpy(to, from, 1);
from = from+1;
to = to+1;
count = count+1;
}
}
int product(int m, int n){
int sum;
int i;
sum = 0;
for(i = 0; i<n; i++){
sum = sum + m;
}
return sum;
}
void jw_insertion_sort ( void *base, int size, int n, Comparable cmp )
{
unsigned char save[MAX_BUF];
unsigned char *cbase = base;
int i, j;
for ( i = 1;; i++ )
{
if( n < 0)
break;
else if( n == 0)
break;
if( i > n)
break;
else if( i == n)
break;
low_level_copy ( save, cbase + product(i, size), size );
for ( j = i;; j-- ){
if(j < 0)
break;
else if(j == 0)
break;
else if(cmp ( (cbase + product( j - 1 , size )), save ) < 0)
break;
else if(cmp ( (cbase + product( j - 1 , size )), save ) == 0)
break;
low_level_copy ( cbase + product( j , size ), cbase + product( j - 1 , size ), size );
}
low_level_copy ( cbase + product( j , size ), save, size );
}
}
void swap_block ( void *a, void *b, int size )
{
if(size < 0)
return;
else if(size == 0)
return;
unsigned char save_block[MAX_BUF];
low_level_copy ( save_block, a, size );
low_level_copy ( a, b, size );
low_level_copy ( b, save_block, size );
}
void jw_quick_sort ( void *base, int size, int low, int high, Comparable cmp )
{
if(high < low)
return;
else if(high == low)
return;
unsigned char save[MAX_BUF];
unsigned char *cbase = base;
int left = low, right = high + 1;
int r;
if ( high - low < CUTOFF )
return;
r = (int) ( ( (double)rand() * ( high - low ) ) / (double)RAND_MAX + low );
swap_block ( cbase + product( low , size ), cbase + product( r , size ), size );
low_level_copy ( save, cbase + product( low , size ), size );
for ( ; ; ) {
while (1){
left = left + 1;
if(left > high)
break;
else if(cmp ( cbase + product( left , size ), save ) > 0)
break;
else if(cmp ( cbase + product( left , size ), save ) == 0)
break;
}
while (1){
right = right - 1;
if(right < 0)
break;
else if(cmp ( cbase + product( right , size ), save ) < 0 )
break;
else if(cmp ( cbase + product( right , size ), save ) == 0 )
break;
}
if ( left > right )
break;
swap_block ( cbase + product( left , size ), cbase + product( right , size ), size );
}
swap_block ( cbase + product( low , size ), cbase + product( right , size ), size );
jw_quick_sort ( base, size, low, right - 1, cmp );
jw_quick_sort ( base, size, right + 1, high, cmp );
}
void jw_sort ( void *base, int size, int n, Comparable cmp )
{
jw_quick_sort ( base, size, 0, n - 1, cmp );
jw_insertion_sort ( base, size, n, cmp );
}
int compare ( const void *a, const void *b ) //improve this function
{
int *aa = (int *)a;
int *bb = (int *)b;
if ( *aa < *bb )
return -1;
else if ( *aa > *bb )
return +1;
return 0;
}
/*****************************************************************************
Do not touch main function
*****************************************************************************/
int main ( void )
{
int a0[10];
int *a1 = malloc ( 1000000 * sizeof ( int ) );
int i, n;
clock_t start;
double jw_time;
for ( i = 0; i < 1000000; i++ )
a1[i] = rand();
start = clock();
jw_sort ( a1, sizeof ( int ), 1000000, compare );
jw_time = ( (double)clock() - start ) / CLOCKS_PER_SEC;
printf ( "Total time consumed in sorting : %f second \n", jw_time );
free ( a1 );
return 0;
}
View Answers
Ads
Related Tutorials/Questions & Answers:
Advertisements
output of the program
output of the program public static int sum(List list)
{
int sum = 0;
for ( Iterator iter = list.iterator(); iter.hasNext(); )
{
int i = ((Integer)iter.next()).intValue();
sum += i;
}
return sum
I/O Program output error
I/O
Program output error Hello All,
I am working on a
program that requries me to read and analyze a .txt file and
output the results to a .txt file, but I am getting incorrect
output. I have been successfull with part
How to get the output of jsp program using Bean
How to get the
output of jsp
program using Bean Hello my Roseindia... the ouput.i want the
output of the
program .i posted the same
program below so... already did that one. Now i want the
output of the
program ,so please tell me
How to get the output of JSP program using Bean
How to get the
output of JSP
program using Bean Hi
Kindly go through a simple
program below and give the solution for the said below one... want the
output of the
program ,so please tell me the Solution Please
output
output Sir,I am experiencing some problems with the
output of this program.I got some useful help this website,but the
output of the
program isn't producing what it should.let me explain the
output below is what happen when
Change Email
Change Email Hi, I need to
change my Rose India register email address for receive email, How did it
possible
Java Change File-Extension
Java
Change File-Extension
In this
program you will learn how to
change the file
extension...;}
}
Output of the
program:
C:\java-examples>
Is it possible in SWT ?
Is it
possible in SWT ? I want drop down like google search (ie, when we type one letter then the word start with that are displayed). when the drop down list appear, then we can select one of word as our text box value.
Can i
ModuleNotFoundError: No module named 'possible'
ModuleNotFoundError: No module named '
possible' Hi,
My Python
program is throwing following error:
ModuleNotFoundError: No module named '
possible'
How to remove the ModuleNotFoundError: No module named '
possible
Example program to change Date formatting Symbols
Example
program to
change Date formatting Symbols... such examples. Now you will
learn how we can
change the formatting symbols for date... it with the java
command. You will get the following
output on your command prompt.
Output
JSP-parameters possible ways
JSP-parameters
possible ways In a JSP
program called books.jsp, the Java code
out.print(request.getParameter(â??numberâ??));
displays â??1111111â??. What are the ways for the parameter number to have got its value
JSP-parameters possible ways
JSP-parameters
possible ways In a JSP
program called books.jsp, the Java code
out.print(request.getParameter(â??numberâ??));
displays â??1111111â??. What are the ways for the parameter number to have got its value
shortest path from the given source to dstination
shortest path from the given source to dstination Hi. Here in my databse.
CREATE TABLE b (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
source... to find the
shortest path from the given source to destination based
program
program write a
program reversee a string.
input-hi this is roseindia
output-roseindia is this hi
Shortest path in JSP for a given source and destination
Shortest path in JSP for a given source and destination Hi. Here in my databse.
CREATE TABLE b ( id int(10) unsigned NOT NULL AUTO_INCREMENT... the
shortest path from the given source to destination based on the databse values. I
Shortest path in JSP for a given source and destination
Shortest path in JSP for a given source and destination Hi. Here in my databse.
CREATE TABLE b ( id int(10) unsigned NOT NULL AUTO_INCREMENT... the
shortest path from the given source to destination based on the databse
program
program i want a progra in java to print a sentence in alphabetic order, taking the input from the user.the
program should writen without using the array
for example : if input= this is a cat
then
output sould = a cat
possible loss of precision error
possible loss of precision error how do i fix this error "
possible loss of precision found int required char ch = input.read()"
this is the code
import java.io.InputStreamReader;
import java.io.IOException;
class Test1
program
program no i want a
program that shows how merge sort algorithm... should also be shown and finally sorted
output values should be printed in rectangles or circles wat ever it may be. the theme of
program is to show algorithm
change url string
change url string How can i
change the login parameter in url after login.That parameter which i were entered will not be shown on url it will be changed after login.I want this.I know it is
possible but tell me the way
program
program Write a
program to print details of a week for a particular day using array of objects Integer and String.
OUTPUT:
First Day of Week is Monday
Second Day of Week is Tuesday
Third Day of Week is Wednesday
Forth Day