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
Related Tutorials/Questions & Answers:
Advertisements
output of the programoutput 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
it possible or notit
possible or not without public static void main(string args[]) in java
program
it is
possible need help with java program as soon as possibleneed help with java
program as soon as possible So my assignment is to write a
program that used while loops to perform 6 different things.
1... than secondNum).
2.
output all the odd numbers between firstNum and secondNum
I/O Program output errorI/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 BeanHow 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 BeanHow 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
plz explain me the output of program - Java Beginnersplz explain me the
output of program Hi friends,
can u please explain me the
output of below
program with proper explanation of each and every line...;Hi Friend,
Output of this
program is:
a=2
b=0
c=0
mul=27
volume=18
outputoutput 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 EmailChange Email Hi, I need to
change my Rose India register email address for receive email, How did it
possible 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
Java Change File-Extension
Java
Change File-Extension
In this
program you will learn how to
change the file
extension...;}
}
}
Output of the
program:ADS_TO_REPLACE_2
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 shortest path from the given source to dstinationshortest 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
Shortest path in JSP for a given source and destinationShortest 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
JSP-parameters possible waysJSP-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 waysJSP-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
triangle outputtriangle output
program to get the following
output:
*
*
Hi Friend,
Please specify the pattern properly.ADS_TO_REPLACE_1
Thanks
Hi Friend..IS IT POSSIBLE? - Java BeginnersHi Friend..IS IT
POSSIBLE? Hi Friend...Thank u for ur valuable response..
IS IT
POSSIBLE FOR US?
I need to display the total Number od days by Each month...
WAIT I WILL SHOW THE
OUTPUT programprogram write a
program reversee a string.
input-hi this is roseindia
output-roseindia is this hi
possible loss of precision errorpossible 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
possible loss of precisionpossible loss of precision {
int amount=(int)depositAmount;
switch (depositAmount)
{
case(depositAmount<=depAmount1... is" +intrest);
}
i have
possible loss of precision as my error
Output of flexOutput of flex hi.......
please provide the name of the
output file.
What is the
output of flex applications?
please rply ASAP........
Thanks
array find all possible dimensionarray find all
possible dimension Given array:
int[] dimension = { 1, 2, 3 };
That produces the
output:
1
2
3
12
13
23
123
I'm new to array...i can't figure out the way on even how to start...
any idea?...really appreaciate
output erroroutput error this is my
program
import java.io.*;
public class separate
{
public static void main(String[] args)throws IOException...];
for (k=0;k
}
}
if i enter "my name" as the input the
output change url stringchange 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
output variableoutput variable if i want to print
output of three variables when i wrote the code e.g SYStem.out.PRINTln(answer + answer1 + answer 2) only
output of answer is getting printed out? can you help
color changecolor change how to
change the color in circle using scrollbar
change passwordchange password how to
change password in the login form.... by giving options to user like this old password, new password.. pls help
what is the output?what is the
output? public class MyAr{
public static void main(String argv[])
{
int[] i=new int[5];
System.out.println(i[5]);
}
}
...
Program is:
public class MyAr{
public static void main(String argv
programprogram 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
ModuleNotFoundError: No module named 'change'ModuleNotFoundError: No module named '
change' Hi,
My Python
program is throwing following error:
ModuleNotFoundError: No module named '
change'
How to remove the ModuleNotFoundError: No module named '
change'
programprogram 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
programprogram 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
input output for any input/
output
operations.
Classes Defined in the
program:
OnlyExt... Input And
Output
Introduction
The Java I/O means Java Input/
Output and is a part