SlideShare a Scribd company logo
I am trying to implement timing on this program and cannot do it. Where am I wrong at?
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void swap(int *x, int *y) {
if (x == y)
return;
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
void cocktailSort(int *a, size_t n) {
while (1) {
char flag;
size_t start[2] = {1, n - 1}, end[2] = {n, 0}, inc[2] = {1, -1};
for (int it = 0; it < 2; it++) {
flag = 1;
for (int i = start[it]; i != end[it]; i += inc[it])
if (a[i - 1] > a[i]) {
swap(a + i - 1, a + i);
flag = 0;
}
if (flag)
return;
}
}
}
int main() {
int array[] = {5, -1, 101, -4, 0, 1, 8, 6, 2, 3};
size_t n = sizeof(array) / sizeof(array[0]);
printf("Original Array: n");
for (int i = 0; i < n; i++)
printf("%d%s", array[i], i == n - 1 ? " n" : " ");
printf("nSorted Array: n");
cocktailSort(array, n);
for (int i = 0; i < n; ++i)
printf("%d ", array[i]);
return 0;
}
void timeAllSorts() {
printf("All Times in milliseconds.n");
printf("| %7s | %10s |n", "Size", "Cocktail");
int size = 8;
while (size <= 131072) {
double c = timeASingleSort(cocktailSort, size);
printf("| %7d | %10.4f |n", size, c);
size = size * 2;
}
}
double timeASingleSort(void (*func)(int *, int), int size) {
int *T = malloc(size * sizeof(int));
for (int i = 0; i < size; i++) {
T[i] = rand() % (size * 4);
}
double before = (double)clock();
func(T, size);
double after = (double)clock();
free(T);
return (after - before) / (CLOCKS_PER_SEC / 1000);
}

More Related Content

DOCX
PRACTICAL COMPUTING
PDF
Aplikasi menghitung matematika dengan c++
DOC
Daapracticals 111105084852-phpapp02
KEY
Sbaw090519
TXT
Snake.c
PDF
Hello, I need some assistance in writing a java program THAT MUST US.pdf
DOCX
DAA Lab File C Programs
PRACTICAL COMPUTING
Aplikasi menghitung matematika dengan c++
Daapracticals 111105084852-phpapp02
Sbaw090519
Snake.c
Hello, I need some assistance in writing a java program THAT MUST US.pdf
DAA Lab File C Programs

Similar to I am trying to implement timing on this program and cannot do it. Wh.pdf (20)

DOC
Sorting programs
PDF
how can i build this as problem 2 of my code #include iostream.pdf
PPT
Lập trình C
PDF
#include stdio.h #include stdlib.h #include unistd.h .pdf
DOCX
Operating system and embedded systems
DOC
Ds program-print
DOCX
PDF
Clock For My
DOCX
Computer Graphics Lab File C Programs
PDF
pattern-printing-in-c.pdf
PDF
PDF
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
DOCX
Binary addition using class concept in c++
PDF
Building Functional Islands
PDF
Pattern printing-in-c(Jaydip Kikani)
PDF
Please help with this JAVA Assignment and show output if you can ple.pdf
DOCX
Os lab file c programs
PDF
I need to find run time analysis and description of the algo.pdf
Sorting programs
how can i build this as problem 2 of my code #include iostream.pdf
Lập trình C
#include stdio.h #include stdlib.h #include unistd.h .pdf
Operating system and embedded systems
Ds program-print
Clock For My
Computer Graphics Lab File C Programs
pattern-printing-in-c.pdf
Project_Euler_No_104_Pandigital_Fibonacci_ends
 
Binary addition using class concept in c++
Building Functional Islands
Pattern printing-in-c(Jaydip Kikani)
Please help with this JAVA Assignment and show output if you can ple.pdf
Os lab file c programs
I need to find run time analysis and description of the algo.pdf

More from allystraders (20)

PDF
Supplemental knowledge conversion processes have been added to the E.pdf
PDF
Supongamos que Musashi, un economista de un programa de radio AM, y .pdf
PDF
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
PDF
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
PDF
Suppose researchers are about to draw a sample of 1450 observations .pdf
PDF
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdf
PDF
Suppose that there are two groups of people in the economy. In group.pdf
PDF
Suppose that the Fed will increase the money supply. Which of the fo.pdf
PDF
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdf
PDF
Suppose that in a particular country, the TFR fell to zero and remai.pdf
PDF
Suppose that disposable income, consumption, and saving in some coun.pdf
PDF
Suppose that 60 of students at Kansas State University have listene.pdf
PDF
Suppose Maria and Jamal both face the following individual loss dist.pdf
PDF
Suppose a random variable X has the following probability distributi.pdf
PDF
Suppose a country had a smaller increase in debt in 2011 than it had.pdf
PDF
how would implement empowerment techniques within a service Choose .pdf
PDF
How were the management controls at Siemens prior to the Bribery sca.pdf
PDF
How would I create this diagramRentTool Viewpoints � Technology .pdf
PDF
How well did Thaldorf interact with each member of the DMUOn what.pdf
PDF
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdf
Supplemental knowledge conversion processes have been added to the E.pdf
Supongamos que Musashi, un economista de un programa de radio AM, y .pdf
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
Suppose researchers are about to draw a sample of 1450 observations .pdf
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdf
Suppose that there are two groups of people in the economy. In group.pdf
Suppose that the Fed will increase the money supply. Which of the fo.pdf
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdf
Suppose that in a particular country, the TFR fell to zero and remai.pdf
Suppose that disposable income, consumption, and saving in some coun.pdf
Suppose that 60 of students at Kansas State University have listene.pdf
Suppose Maria and Jamal both face the following individual loss dist.pdf
Suppose a random variable X has the following probability distributi.pdf
Suppose a country had a smaller increase in debt in 2011 than it had.pdf
how would implement empowerment techniques within a service Choose .pdf
How were the management controls at Siemens prior to the Bribery sca.pdf
How would I create this diagramRentTool Viewpoints � Technology .pdf
How well did Thaldorf interact with each member of the DMUOn what.pdf
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdf

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Types and Its function , kingdom of life
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Presentation on HIE in infants and its manifestations
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
A systematic review of self-coping strategies used by university students to ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Pharmacology of Heart Failure /Pharmacotherapy of CHF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
human mycosis Human fungal infections are called human mycosis..pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Types and Its function , kingdom of life
Anesthesia in Laparoscopic Surgery in India
Presentation on HIE in infants and its manifestations
O7-L3 Supply Chain Operations - ICLT Program
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf

I am trying to implement timing on this program and cannot do it. Wh.pdf

  • 1. I am trying to implement timing on this program and cannot do it. Where am I wrong at? #include "stdio.h" #include "stdlib.h" #include "time.h" void swap(int *x, int *y) { if (x == y) return; *x ^= *y; *y ^= *x; *x ^= *y; } void cocktailSort(int *a, size_t n) { while (1) { char flag; size_t start[2] = {1, n - 1}, end[2] = {n, 0}, inc[2] = {1, -1}; for (int it = 0; it < 2; it++) { flag = 1; for (int i = start[it]; i != end[it]; i += inc[it]) if (a[i - 1] > a[i]) { swap(a + i - 1, a + i); flag = 0; } if (flag) return; } } } int main() { int array[] = {5, -1, 101, -4, 0, 1, 8, 6, 2, 3}; size_t n = sizeof(array) / sizeof(array[0]);
  • 2. printf("Original Array: n"); for (int i = 0; i < n; i++) printf("%d%s", array[i], i == n - 1 ? " n" : " "); printf("nSorted Array: n"); cocktailSort(array, n); for (int i = 0; i < n; ++i) printf("%d ", array[i]); return 0; } void timeAllSorts() { printf("All Times in milliseconds.n"); printf("| %7s | %10s |n", "Size", "Cocktail"); int size = 8; while (size <= 131072) { double c = timeASingleSort(cocktailSort, size); printf("| %7d | %10.4f |n", size, c); size = size * 2; } } double timeASingleSort(void (*func)(int *, int), int size) { int *T = malloc(size * sizeof(int)); for (int i = 0; i < size; i++) { T[i] = rand() % (size * 4); } double before = (double)clock(); func(T, size); double after = (double)clock();
  • 3. free(T); return (after - before) / (CLOCKS_PER_SEC / 1000); }