Shortest the program as possible but output will not change

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:
Shortest the program as possible but output will not change
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
Shortest the program as possible but output will not change
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
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
it possible or not
it possible or not  without public static void main(string args[]) in java program it is possible
need help with java program as soon as possible
need 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 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
java program...need help as soon as possible!!! - Java Beginners
java program...need help as soon as possible!!!  Modify the Lexer (15... Be sure to change token file appropriately and then run TokenSetup as required 3.... 4. Print each token with line number READLINE: program { int i int j
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
C Program to Print Following Output - Development process
C Program to Print Following Output  Hello Sir I wnat to print Followning output in C Language with for loop How I Can Print it? 5 5 4 5 4 3 5 4 3 2 5 4 3 2 1  Hi Friend, Try the following: #include
plz explain me the output of program - Java Beginners
plz 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
c programming..what wil be the output of this program
c programming..what wil be the output of this program   #include int main() { int arr[] = {0,1,2,3,4}; int i,*ptr; for (ptr=arr+4; ptr= arr; ptr--) { printf("%d",*ptr
How to print the following output using c program
How to print the following output using c program  1) 4 3 4 2 3 4 1 2 3 4 2) A B C D E F G H I J
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
Not getting desired output while button is pressed in java applet program
Not getting desired output while button is pressed in java applet program  //The code is as follows,i want that whatever data is there in text field T1 and T2 get subtracted and comes in T3 after we press the button.../// import
Write a program using JSP that enables any student to change his/her address.
Write a program using JSP that enables any student to change his/her address.  Write a program using JSP that enables any student to change his/her address
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?
write a program in java to read a text file and write the output to an excel file using filereader and filewriter?  write a program in java to read a text file and write the output to an excel file using filereader and filewriter
Change orientation of UIViewController
Change orientation of UIViewController  Is it possible to change the UIViewController orientation dynamically
Change Email
Change 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 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
triangle output
triangle output  program to get the following output
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
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
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... the following output on your command prompt. Output:  C
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
triangle output
triangle 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 Beginners
Hi 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
program
program  write a program reversee a string. input-hi this is roseindia output-roseindia is this hi
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
possible loss of precision
possible 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 flex
Output 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 dimension
array 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 error
output 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 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
output variable
output 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 change
color change  how to change the color in circle using scrollbar
change password
change 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
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
Is it possible to upgrade mysql 5.1 to 5.5
Is it possible to upgrade mysql 5.1 to 5.5  Is it possible to upgrade mysql 5.1 to 5.5
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'
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
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
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
ModuleNotFoundError: No module named 'possible-english-words'
ModuleNotFoundError: No module named 'possible-english-words'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'possible-english-words' How to remove the ModuleNotFoundError
ModuleNotFoundError: No module named 'possible-words-siramkalyan'
ModuleNotFoundError: No module named 'possible-words-siramkalyan'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'possible-words-siramkalyan' How to remove

Ads