Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa IoC Shortest the program as possible but output will not change
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Chinmaya Naik
Shortest the program as possible but output will not change
0 Answer(s)      a year and 8 months ago
Posted in : IoC


#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;
}

Advertisement
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
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
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
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
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
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
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
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
Change Email
Change Email  Hi, I need to change my Rose India register email address for receive email, How did it possible
triangle output
triangle output  program to get the following output
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>
Change orientation of UIViewController
Change orientation of UIViewController  Is it possible to change the UIViewController orientation dynamically
triangle output
triangle output  program to get the following output: * *   Hi Friend, Please specify the pattern properly. Thanks
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
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
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
 

 

 

DMCA.com