SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
Massachusetts Institute of Technology

Department of Electrical Engineering and Computer Science

6.087: Practical Programming in C
IAP 2010
Problem Set 4
Pointers. Arrays. Strings. Searching and sorting algorithms.
Out: Friday, January 15, 2010.	 Due: Tuesday, January 19, 2010.
Problem 4.1
(a) The function shift element() takes as input the index of an array element that has been

determined to be out of order. The function shifts the element towards the front of the array,

repeatedly swapping the preceding element until the out-of-order element is in the proper

location. The implementation using array indexing is provided below for your reference:

/∗ move previous elements down u n t i l

i n s e r t i o n point reached ∗/

void s h i f t e l e m e n t (unsigned int i ) {

int ivalue ;

/∗	 guard against going outside array ∗/
for ( ival ue = arr [ i ] ; i && arr [ i −1] > iva lue ; i −−)

arr [ i ] = arr [ i −1]; move element down
/∗ ∗/

arr [ i ] = i valu e ; /∗ i n s e r t element ∗/

}
Re-implement this function using pointers and pointer arithmetic instead of array indexing.
/∗	 int ∗pElement − pointer to the element

in arr ( type int [ ] ) that i s out−of−place
 ∗/
void s h i f t e l e m e n t ( int ∗pElement ) {
/∗	 i n s e r t code here ∗/
}
(b) The function insertion sort() contains the main loop of the algorithm. It iterates through

elements of the array, from the beginning, until it reaches an element that is out-of-order. It

calls shift element() to shift the offending element to its proper location earlier in the array

and resumes iterating until the end is reached. The code from lecture is provided below:

/∗	 i t e r a t e u n t i l out−of−order element found ;

s h i f t the element , and continue i t e r a t i n g ∗/

void i n s e r t i o n s o r t ( void ) {

unsigned int i , len = array length ( arr ) ;

for ( i = 1; i < len ; i++)

i f ( arr [ i ] < arr [ i −1])

s h i f t e l e m e n t ( i ) ;

}
Re-implement this function using pointers and pointer arithmetic instead of array indexing.
Use the shift element() function you implemented in part (a).
1
info@programmingassignments.com
info@programmingassignments.com
will	re­implement
the	algorithm	using	pointers	and	pointer	arithmetic.
Consider	the	insertion	sort	algorithm.	In	this	problem,	you
Submit Assignment For Help
Go To Code Diterctly
Problem 4.2
In this problem, we will use our knowledge of strings to duplicate the functionality of the C standard
library’s strtok() function, which extracts “tokens” from a string. The string is split using a set of
delimiters, such as whitespace and punctuation. Each piece of the string, without its surrounding
delimiters, is a token. The process of extracting a token can be split into two parts: finding the
beginning of the token (the first character that is not a delimiter), and finding the end of the token
(the next character that is a delimiter). The first call of strtok() looks like this:
char ∗ strtok(char ∗ str, const char ∗ delims);
The string str is the string to be tokenized, delims is a string containing all the single characters
to use as delimiters (e.g. " trn"), and the return value is the first token in str. Additional
tokens can be obtained by calling strtok() with NULL passed for the str argument:
char ∗ strtok(NULL, const char ∗ delims);
Because strtok() uses a static variable to store the pointer to the beginning of the next token,
calls to strtok() for different strings cannot be interleaved. The code for strtok() is provided
below:
char ∗ strtok ( char ∗ text , const char ∗ delims ) {
/∗	 i n i t i a l i z e ∗/
i f	 ( ! text )
text = pnexttoken ;
/∗	 find s t a r t of token in text ∗/
text += strspn ( text , delims ) ;
i f (∗ text == ’0’)
return NULL;
/∗	 find end of token in text ∗/
pnexttoken = text + strcspn ( text , delims ) ;
/∗	 i n s e r t null −terminator at end ∗/
i f (∗ pnexttoken != ’0’)
∗ pnexttoken++ = ’0’ ;

return text ;

}
(a) In the context of our string tokenizer, the function	 strspn() computes the index of the
first non-delimiter character in our string. Using pointers or array indexing (your choice),
implement the strspn() function. In order to locate a character in another string, you may
use the function strpos(), which is declared below:
int	 strpos ( const char str , const char ch ) ;∗
This function returns the index of the first occurrence of the character ch in the string str,
or -1 if ch is not found. The declaration of strspn() is provided below:
unsigned int strspn ( const char ∗ str , const char ∗ delims ) {
/∗	 i n s e r t code here ∗/
}
Here, delims is a string containing the set of delimiters, and the return value is the index of the
first non-delimiter character in the string str. For instance, strspn(" . This", " .") == 3.
If the string contains only delimiters, strspn() should return the index of the null-terminator
(’0’). Assume ’0’ is not a delimiter.
2
(b) The function strcspn() computes the index of the first delimiter character in our string.
Here’s the declaration of strcspn():
unsigned int strcspn ( const char ∗ str , const char ∗ delims ) {
/∗	 i n s e r t code here ∗/
}
If the string contains no delimiters, return the index of the null-terminator (’0’). Implement
this function using either pointers or array indexing.
Problem 4.3
In this problem, you will be implementing the shell sort. This sort is built upon the insertion sort,
but attains a speed increase by comparing far-away elements against each other before comparing
closer-together elements. The distance between elements is called the “gap”. In the shell sort, the
array is sorted by sorting gap sub-arrays, and then repeating with a smaller gap size.
As written here, the algorithm sorts in O(n2) time. However, by adjusting the sequence of
gap sizes used, it is possible to improve the performance to O(n3/2), O(n4/3), or even O(n(log n)2)
time. You can read about a method for performing the shell sort in O(n(log n)2) time on Robert
Sedgewick’s page at Princeton:
http://www.cs.princeton.edu/~rs/shell/paperF.pdf
Note that you can find complete C code for the shell sort at the beginning of the paper, so please
wait until you have finished this exercise to read it.
(a) First, we will modify the shift element() function from the insertion sort code for the shell
sort. The new function, started for you below, should start at index i and shift by intervals
of size gap. Write the new function, using array indexing or pointers. Assume that i ≥ gap.
void shift element by gap ( unsigned int i , unsigned int gap ) {
/∗	 i n s e r t code here ∗/
}
(b) Now, we need to write the main shell sort routine. Using the template below, fill in the missing
code so that the insertion sort in the inner loop compares every element to the element gap
spaces before it.
void s h e l l s o r t ( void ) {

unsigned int gap , i ,

len = array length ( arr ) ;

/∗	 sort , comparing against f a r t h e r away

elements f i r s t , then c l o s e r elements ∗/

for ( gap = len /2; gap > 0; gap /= 2) {
/∗ do i n s e r t i o n −l i k e sort , but comparing

and s h i f t i n g elements in multiples of gap ∗/

for i n s e r t code here
( /∗	 ∗/ ) {
i f ( /∗ i n s e r t code here ∗/ ) {

/∗ out of order , do s h i f t ∗/

shift element by gap ( i , gap ) ;
}

}

}

}

3
Massachusetts Institute of Technology

Department of Electrical Engineering and Computer Science

6.087: Practical Programming in C
IAP 2010
Problem Set 4 – Solutions
Pointers. Arrays. Strings. Searching and sorting algorithms.
Out: Friday, January 15, 2010.	 Due: Tuesday, January 19, 2010.
Problem 4.1
Consider the insertion sort algorithm described in Lecture 5 (slides 21-23). In this problem, you
will re-implement the algorithm using pointers and pointer arithmetic.
(a) The function shift element() takes as input the index of an array element that has been
determined to be out of order. The function shifts the element towards the front of the array,
repeatedly swapping the preceding element until the out-of-order element is in the proper
location. The implementation using array indexing is provided below for your reference:
/∗ move previous elements down u n t i l

i n s e r t i o n point reached ∗/

void s h i f t e l e m e n t (unsigned int i ) {

int ivalue ;

/∗	 guard against going outside array ∗/
for ( ival ue = arr [ i ] ; i && arr [ i −1] > iva lue ; i −−)

arr [ i ] = arr [ i −1]; move element down
/∗ ∗/

arr [ i ] = i value ; /∗ i n s e r t element ∗/

}
Re-implement this function using pointers and pointer arithmetic instead of array indexing.
/∗	 int ∗pElement − pointer to the element

in arr ( type int [ ] ) that i s out−of−place
 ∗/
void s h i f t e l e m e n t ( int ∗pElement ) {
/∗	 i n s e r t code here ∗/
}
Answer: Here’s one possible implementation:
/∗	 int ∗pElement − pointer to the element

in arr ( type int [ ] ) that i s out−of−place
 ∗/
void s h i f t e l e m e n t ( int ∗pElement ) {
int ivalue = ∗pElement ;
for ( ival ue = ∗pElement ; pElement > arr && ∗( pElement −1) > iva lue ; pElement−−)
∗pElement = ∗( pElement −1);
∗pElement = ivalue ;
}
(b) The function insertion sort() contains the main loop of the algorithm. It iterates through
elements of the array, from the beginning, until it reaches an element that is out-of-order. It
calls shift element() to shift the offending element to its proper location earlier in the array
and resumes iterating until the end is reached. The code from lecture is provided below:
1
/∗ i t e r a t e u n t i l out−of−order element found ;
s h i f t the element , and continue i t e r a t i n g ∗/
void i n s e r t i o n s o r t ( void ) {
unsigned int i , len = array length ( arr ) ;
for ( i = 1; i < len ; i++)
i f ( arr [ i ] < arr [ i −1])
s h i f t e l e m e n t ( i ) ;
}
Re-implement this function using pointers and pointer arithmetic instead of array indexing.

Use the shift element() function you implemented in part (a).

Answer: Here’s one possible implementation:

/∗ i t e r a t e u n t i l out−of−order element found ;
s h i f t the element , and continue i t e r a t i n g ∗/
void i n s e r t i o n s o r t ( void ) {
int pElement , ∗pEnd = arr + array length ( arr ) ;∗
for ( pElement = arr +1; pElement < pEnd ; pElement++)

i f (∗ pElement < ∗( pElement −1))

s h i f t e l e m e n t ( pElement ) ;

}
Problem 4.2
In this problem, we will use our knowledge of strings to duplicate the functionality of the C standard
library’s strtok() function, which extracts “tokens” from a string. The string is split using a set of
delimiters, such as whitespace and punctuation. Each piece of the string, without its surrounding
delimiters, is a token. The process of extracting a token can be split into two parts: finding the
beginning of the token (the first character that is not a delimiter), and finding the end of the token
(the next character that is a delimiter). The first call of strtok() looks like this:
char ∗ strtok(char ∗ str, const char ∗ delims);
The string str is the string to be tokenized, delims is a string containing all the single characters
to use as delimiters (e.g. " trn"), and the return value is the first token in str. Additional
tokens can be obtained by calling strtok() with NULL passed for the str argument:
char ∗ strtok(NULL, const char ∗ delims);
Because strtok() uses a static variable to store the pointer to the beginning of the next token,
calls to strtok() for different strings cannot be interleaved. The code for strtok() is provided
below:
char ∗ strtok ( char ∗ text , const char ∗ delims ) {
/∗ i n i t i a l i z e ∗/
i f ( ! text )
text = pnexttoken ;
/∗ find s t a r t of token in text ∗/
text += strspn ( text , delims ) ;
i f (∗ text == ’0’)
return NULL;
/∗ find end of token in text ∗/
pnexttoken = text + strcspn ( text , delims ) ;
/∗ i n s e r t null −terminator at end ∗/
i f (∗ pnexttoken != ’0’)
∗ pnexttoken++ = ’0’ ;

return text ;

}
2
(a) In the	 context of our string tokenizer, the function strspn() computes the index of the
first non-delimiter character in our string. Using pointers or array indexing (your choice),
implement the strspn() function. In order to locate a character in another string, you may
use the function strpos(), which is declared below:
int	 strpos ( const char str , const char ch ) ;∗
This function returns the index of the first occurrence of the character ch in the string str,
or -1 if ch is not found. The declaration of strspn() is provided below:
unsigned int strspn ( const char ∗ str , const char ∗ delims ) {
/∗	 i n s e r t code here ∗/
}
Here, delims is a string containing the set of delimiters, and the return value is the index of the
first non-delimiter character in the string str. For instance, strspn(" . This", " .") == 3.
If the string contains only delimiters, strspn() should return the index of the null-terminator
(’0’). Assume ’0’ is not a delimiter.
Answer: Here’s a simple implementation of strspn():
unsigned int strspn ( const char ∗ str , const char ∗ delims ) {

unsigned int i ;

for ( i = 0; s t r [ i ] != ’0’ && strpos ( delims , s t r [ i ] ) > −1; i ++);

return i ;

}
(b) The function	 strcspn() computes the index of the first delimiter character in our string.
Here’s the declaration of strcspn():
unsigned int strcspn ( const char ∗ str , const char ∗ delims ) {
/∗	 i n s e r t code here ∗/
}
If the string contains no delimiters, return the index of the null-terminator (’0’). Implement

this function using either pointers or array indexing.

Answer: Here’s a simple implementation of strcspn():

unsigned int strcspn ( const char ∗ str , const char ∗ delims ) {

unsigned int i ;

for ( i = 0; s t r [ i ] != ’0’ && strpos ( delims , s t r [ i ] ) == −1; i ++);

return i ;

}
Problem 4.3
In this problem, you will be implementing the shell sort. This sort is built upon the insertion sort,
but attains a speed increase by comparing far-away elements against each other before comparing
closer-together elements. The distance between elements is called the “gap”. In the shell sort, the
array is sorted by sorting gap sub-arrays, and then repeating with a smaller gap size.
As written here, the algorithm sorts in O(n2) time. However, by adjusting the sequence of
gap sizes used, it is possible to improve the performance to O(n3/2), O(n4/3), or even O(n(log n)2)
time. You can read about a method for performing the shell sort in O(n(log n)2) time on Robert
Sedgewick’s page at Princeton:
3
http://www.cs.princeton.edu/~rs/shell/paperF.pdf

Note that you can find complete C code for the shell sort at the beginning of the paper, so please
wait until you have finished this exercise to read it.
(a) First, we will modify the shift element() function from the insertion sort code for the shell
sort. The new function, started for you below, should start at index i and shift by intervals
of size gap. Write the new function, using array indexing or pointers. Assume that i ≥ gap.
void shift element by gap ( unsigned int i , unsigned int gap ) {
/∗ i n s e r t code here ∗/
}
Answer: One possible version of this function is:
void shift element by gap ( unsigned int i ,
unsigned int gap ) {
int ivalue ;
unsigned int j ;
for ( j = i , iva lue = arr [ i ] ; j >= gap
&& arr [ j−gap ] > ivalue ; j −= gap )
arr [ j ] = arr [ j−gap ] ; move/∗ ∗/
arr [ j ] = iva lue ; /∗ i n s e r t element ∗/
}
(b) Now, we need to write the main shell sort routine. Using the template below, fill in the missing
code so that the insertion sort in the inner loop compares every element to the element gap
spaces before it.
void s h e l l
unsigned
s o r t ( void ) {
int gap , i ,
len = array length ( arr ) ;
/∗ sort , comparing against f a r t h e r away
elements f i r s t , then c l o s e r elements ∗/
for ( gap = len /2; gap > 0; gap /= 2) {
/∗ do i n s e rt i o n −l i k e sort , but comparing
and s h i f t i n g elements in multiples of gap ∗/
for ( /∗ i n s e r t code here ∗/ ) {
i f ( /∗ i n s e r t code here ∗/ ) {
/∗ out of order , do s h i f t ∗/
shift element by gap ( i , gap ) ;
}
}
}
}
Answer: One possible solution is shown below:
void s h e l l
unsigned
s o r t ( void ) {
int gap , i , len = array length ( arr ) ;
/∗ compare against f a r t h e r away elements f i r s t , then c l o s e r elements ∗/
for ( gap = len /2; gap > 0; gap /= 2)
/∗ comparing and s h i f t i n g elements
{
in multiples of gap ∗/
for ( i = gap ; i < len ; i++)
i f ( arr [ i−gap ] > arr [ i ] )
shift element by gap ( i , gap ) ; /∗ out of order , do s h i f t ∗/
}
}
4

Mais conteúdo relacionado

Mais procurados

Mais procurados (16)

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Advance topics of C language
Advance  topics of C languageAdvance  topics of C language
Advance topics of C language
 
Pointer
PointerPointer
Pointer
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
8 Pointers
8 Pointers8 Pointers
8 Pointers
 
9 Arrays
9 Arrays9 Arrays
9 Arrays
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 
C q 3
C q 3C q 3
C q 3
 
Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING10. funtions and closures IN SWIFT PROGRAMMING
10. funtions and closures IN SWIFT PROGRAMMING
 

Semelhante a C programming assignment help

Data structures arrays
Data structures   arraysData structures   arrays
Data structures arraysmaamir farooq
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxdudelover
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 
ESC112 Course lecture slides on pointers and memory allocation.pptx
ESC112 Course lecture slides on pointers and memory allocation.pptxESC112 Course lecture slides on pointers and memory allocation.pptx
ESC112 Course lecture slides on pointers and memory allocation.pptxkrishna50blogging
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptshashankbhadouria4
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stackecomputernotes
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arraysphanleson
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumarSujith Kumar
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Appili Vamsi Krishna
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
Array and Pointers
Array and PointersArray and Pointers
Array and PointersProf Ansari
 

Semelhante a C programming assignment help (20)

Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Time and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptxTime and Space Complexity Analysis.pptx
Time and Space Complexity Analysis.pptx
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
ESC112 Course lecture slides on pointers and memory allocation.pptx
ESC112 Course lecture slides on pointers and memory allocation.pptxESC112 Course lecture slides on pointers and memory allocation.pptx
ESC112 Course lecture slides on pointers and memory allocation.pptx
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Python Datatypes by SujithKumar
Python Datatypes by SujithKumarPython Datatypes by SujithKumar
Python Datatypes by SujithKumar
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Strings
StringsStrings
Strings
 
Data Structure
Data StructureData Structure
Data Structure
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Array and Pointers
Array and PointersArray and Pointers
Array and Pointers
 

Último

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 

Último (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 

C programming assignment help

  • 1. Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.087: Practical Programming in C IAP 2010 Problem Set 4 Pointers. Arrays. Strings. Searching and sorting algorithms. Out: Friday, January 15, 2010. Due: Tuesday, January 19, 2010. Problem 4.1 (a) The function shift element() takes as input the index of an array element that has been determined to be out of order. The function shifts the element towards the front of the array, repeatedly swapping the preceding element until the out-of-order element is in the proper location. The implementation using array indexing is provided below for your reference: /∗ move previous elements down u n t i l i n s e r t i o n point reached ∗/ void s h i f t e l e m e n t (unsigned int i ) { int ivalue ; /∗ guard against going outside array ∗/ for ( ival ue = arr [ i ] ; i && arr [ i −1] > iva lue ; i −−) arr [ i ] = arr [ i −1]; move element down /∗ ∗/ arr [ i ] = i valu e ; /∗ i n s e r t element ∗/ } Re-implement this function using pointers and pointer arithmetic instead of array indexing. /∗ int ∗pElement − pointer to the element in arr ( type int [ ] ) that i s out−of−place ∗/ void s h i f t e l e m e n t ( int ∗pElement ) { /∗ i n s e r t code here ∗/ } (b) The function insertion sort() contains the main loop of the algorithm. It iterates through elements of the array, from the beginning, until it reaches an element that is out-of-order. It calls shift element() to shift the offending element to its proper location earlier in the array and resumes iterating until the end is reached. The code from lecture is provided below: /∗ i t e r a t e u n t i l out−of−order element found ; s h i f t the element , and continue i t e r a t i n g ∗/ void i n s e r t i o n s o r t ( void ) { unsigned int i , len = array length ( arr ) ; for ( i = 1; i < len ; i++) i f ( arr [ i ] < arr [ i −1]) s h i f t e l e m e n t ( i ) ; } Re-implement this function using pointers and pointer arithmetic instead of array indexing. Use the shift element() function you implemented in part (a). 1 info@programmingassignments.com info@programmingassignments.com will re­implement the algorithm using pointers and pointer arithmetic. Consider the insertion sort algorithm. In this problem, you Submit Assignment For Help Go To Code Diterctly
  • 2. Problem 4.2 In this problem, we will use our knowledge of strings to duplicate the functionality of the C standard library’s strtok() function, which extracts “tokens” from a string. The string is split using a set of delimiters, such as whitespace and punctuation. Each piece of the string, without its surrounding delimiters, is a token. The process of extracting a token can be split into two parts: finding the beginning of the token (the first character that is not a delimiter), and finding the end of the token (the next character that is a delimiter). The first call of strtok() looks like this: char ∗ strtok(char ∗ str, const char ∗ delims); The string str is the string to be tokenized, delims is a string containing all the single characters to use as delimiters (e.g. " trn"), and the return value is the first token in str. Additional tokens can be obtained by calling strtok() with NULL passed for the str argument: char ∗ strtok(NULL, const char ∗ delims); Because strtok() uses a static variable to store the pointer to the beginning of the next token, calls to strtok() for different strings cannot be interleaved. The code for strtok() is provided below: char ∗ strtok ( char ∗ text , const char ∗ delims ) { /∗ i n i t i a l i z e ∗/ i f ( ! text ) text = pnexttoken ; /∗ find s t a r t of token in text ∗/ text += strspn ( text , delims ) ; i f (∗ text == ’0’) return NULL; /∗ find end of token in text ∗/ pnexttoken = text + strcspn ( text , delims ) ; /∗ i n s e r t null −terminator at end ∗/ i f (∗ pnexttoken != ’0’) ∗ pnexttoken++ = ’0’ ; return text ; } (a) In the context of our string tokenizer, the function strspn() computes the index of the first non-delimiter character in our string. Using pointers or array indexing (your choice), implement the strspn() function. In order to locate a character in another string, you may use the function strpos(), which is declared below: int strpos ( const char str , const char ch ) ;∗ This function returns the index of the first occurrence of the character ch in the string str, or -1 if ch is not found. The declaration of strspn() is provided below: unsigned int strspn ( const char ∗ str , const char ∗ delims ) { /∗ i n s e r t code here ∗/ } Here, delims is a string containing the set of delimiters, and the return value is the index of the first non-delimiter character in the string str. For instance, strspn(" . This", " .") == 3. If the string contains only delimiters, strspn() should return the index of the null-terminator (’0’). Assume ’0’ is not a delimiter. 2
  • 3. (b) The function strcspn() computes the index of the first delimiter character in our string. Here’s the declaration of strcspn(): unsigned int strcspn ( const char ∗ str , const char ∗ delims ) { /∗ i n s e r t code here ∗/ } If the string contains no delimiters, return the index of the null-terminator (’0’). Implement this function using either pointers or array indexing. Problem 4.3 In this problem, you will be implementing the shell sort. This sort is built upon the insertion sort, but attains a speed increase by comparing far-away elements against each other before comparing closer-together elements. The distance between elements is called the “gap”. In the shell sort, the array is sorted by sorting gap sub-arrays, and then repeating with a smaller gap size. As written here, the algorithm sorts in O(n2) time. However, by adjusting the sequence of gap sizes used, it is possible to improve the performance to O(n3/2), O(n4/3), or even O(n(log n)2) time. You can read about a method for performing the shell sort in O(n(log n)2) time on Robert Sedgewick’s page at Princeton: http://www.cs.princeton.edu/~rs/shell/paperF.pdf Note that you can find complete C code for the shell sort at the beginning of the paper, so please wait until you have finished this exercise to read it. (a) First, we will modify the shift element() function from the insertion sort code for the shell sort. The new function, started for you below, should start at index i and shift by intervals of size gap. Write the new function, using array indexing or pointers. Assume that i ≥ gap. void shift element by gap ( unsigned int i , unsigned int gap ) { /∗ i n s e r t code here ∗/ } (b) Now, we need to write the main shell sort routine. Using the template below, fill in the missing code so that the insertion sort in the inner loop compares every element to the element gap spaces before it. void s h e l l s o r t ( void ) { unsigned int gap , i , len = array length ( arr ) ; /∗ sort , comparing against f a r t h e r away elements f i r s t , then c l o s e r elements ∗/ for ( gap = len /2; gap > 0; gap /= 2) { /∗ do i n s e r t i o n −l i k e sort , but comparing and s h i f t i n g elements in multiples of gap ∗/ for i n s e r t code here ( /∗ ∗/ ) { i f ( /∗ i n s e r t code here ∗/ ) { /∗ out of order , do s h i f t ∗/ shift element by gap ( i , gap ) ; } } } } 3
  • 4. Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.087: Practical Programming in C IAP 2010 Problem Set 4 – Solutions Pointers. Arrays. Strings. Searching and sorting algorithms. Out: Friday, January 15, 2010. Due: Tuesday, January 19, 2010. Problem 4.1 Consider the insertion sort algorithm described in Lecture 5 (slides 21-23). In this problem, you will re-implement the algorithm using pointers and pointer arithmetic. (a) The function shift element() takes as input the index of an array element that has been determined to be out of order. The function shifts the element towards the front of the array, repeatedly swapping the preceding element until the out-of-order element is in the proper location. The implementation using array indexing is provided below for your reference: /∗ move previous elements down u n t i l i n s e r t i o n point reached ∗/ void s h i f t e l e m e n t (unsigned int i ) { int ivalue ; /∗ guard against going outside array ∗/ for ( ival ue = arr [ i ] ; i && arr [ i −1] > iva lue ; i −−) arr [ i ] = arr [ i −1]; move element down /∗ ∗/ arr [ i ] = i value ; /∗ i n s e r t element ∗/ } Re-implement this function using pointers and pointer arithmetic instead of array indexing. /∗ int ∗pElement − pointer to the element in arr ( type int [ ] ) that i s out−of−place ∗/ void s h i f t e l e m e n t ( int ∗pElement ) { /∗ i n s e r t code here ∗/ } Answer: Here’s one possible implementation: /∗ int ∗pElement − pointer to the element in arr ( type int [ ] ) that i s out−of−place ∗/ void s h i f t e l e m e n t ( int ∗pElement ) { int ivalue = ∗pElement ; for ( ival ue = ∗pElement ; pElement > arr && ∗( pElement −1) > iva lue ; pElement−−) ∗pElement = ∗( pElement −1); ∗pElement = ivalue ; } (b) The function insertion sort() contains the main loop of the algorithm. It iterates through elements of the array, from the beginning, until it reaches an element that is out-of-order. It calls shift element() to shift the offending element to its proper location earlier in the array and resumes iterating until the end is reached. The code from lecture is provided below: 1
  • 5. /∗ i t e r a t e u n t i l out−of−order element found ; s h i f t the element , and continue i t e r a t i n g ∗/ void i n s e r t i o n s o r t ( void ) { unsigned int i , len = array length ( arr ) ; for ( i = 1; i < len ; i++) i f ( arr [ i ] < arr [ i −1]) s h i f t e l e m e n t ( i ) ; } Re-implement this function using pointers and pointer arithmetic instead of array indexing. Use the shift element() function you implemented in part (a). Answer: Here’s one possible implementation: /∗ i t e r a t e u n t i l out−of−order element found ; s h i f t the element , and continue i t e r a t i n g ∗/ void i n s e r t i o n s o r t ( void ) { int pElement , ∗pEnd = arr + array length ( arr ) ;∗ for ( pElement = arr +1; pElement < pEnd ; pElement++) i f (∗ pElement < ∗( pElement −1)) s h i f t e l e m e n t ( pElement ) ; } Problem 4.2 In this problem, we will use our knowledge of strings to duplicate the functionality of the C standard library’s strtok() function, which extracts “tokens” from a string. The string is split using a set of delimiters, such as whitespace and punctuation. Each piece of the string, without its surrounding delimiters, is a token. The process of extracting a token can be split into two parts: finding the beginning of the token (the first character that is not a delimiter), and finding the end of the token (the next character that is a delimiter). The first call of strtok() looks like this: char ∗ strtok(char ∗ str, const char ∗ delims); The string str is the string to be tokenized, delims is a string containing all the single characters to use as delimiters (e.g. " trn"), and the return value is the first token in str. Additional tokens can be obtained by calling strtok() with NULL passed for the str argument: char ∗ strtok(NULL, const char ∗ delims); Because strtok() uses a static variable to store the pointer to the beginning of the next token, calls to strtok() for different strings cannot be interleaved. The code for strtok() is provided below: char ∗ strtok ( char ∗ text , const char ∗ delims ) { /∗ i n i t i a l i z e ∗/ i f ( ! text ) text = pnexttoken ; /∗ find s t a r t of token in text ∗/ text += strspn ( text , delims ) ; i f (∗ text == ’0’) return NULL; /∗ find end of token in text ∗/ pnexttoken = text + strcspn ( text , delims ) ; /∗ i n s e r t null −terminator at end ∗/ i f (∗ pnexttoken != ’0’) ∗ pnexttoken++ = ’0’ ; return text ; } 2
  • 6. (a) In the context of our string tokenizer, the function strspn() computes the index of the first non-delimiter character in our string. Using pointers or array indexing (your choice), implement the strspn() function. In order to locate a character in another string, you may use the function strpos(), which is declared below: int strpos ( const char str , const char ch ) ;∗ This function returns the index of the first occurrence of the character ch in the string str, or -1 if ch is not found. The declaration of strspn() is provided below: unsigned int strspn ( const char ∗ str , const char ∗ delims ) { /∗ i n s e r t code here ∗/ } Here, delims is a string containing the set of delimiters, and the return value is the index of the first non-delimiter character in the string str. For instance, strspn(" . This", " .") == 3. If the string contains only delimiters, strspn() should return the index of the null-terminator (’0’). Assume ’0’ is not a delimiter. Answer: Here’s a simple implementation of strspn(): unsigned int strspn ( const char ∗ str , const char ∗ delims ) { unsigned int i ; for ( i = 0; s t r [ i ] != ’0’ && strpos ( delims , s t r [ i ] ) > −1; i ++); return i ; } (b) The function strcspn() computes the index of the first delimiter character in our string. Here’s the declaration of strcspn(): unsigned int strcspn ( const char ∗ str , const char ∗ delims ) { /∗ i n s e r t code here ∗/ } If the string contains no delimiters, return the index of the null-terminator (’0’). Implement this function using either pointers or array indexing. Answer: Here’s a simple implementation of strcspn(): unsigned int strcspn ( const char ∗ str , const char ∗ delims ) { unsigned int i ; for ( i = 0; s t r [ i ] != ’0’ && strpos ( delims , s t r [ i ] ) == −1; i ++); return i ; } Problem 4.3 In this problem, you will be implementing the shell sort. This sort is built upon the insertion sort, but attains a speed increase by comparing far-away elements against each other before comparing closer-together elements. The distance between elements is called the “gap”. In the shell sort, the array is sorted by sorting gap sub-arrays, and then repeating with a smaller gap size. As written here, the algorithm sorts in O(n2) time. However, by adjusting the sequence of gap sizes used, it is possible to improve the performance to O(n3/2), O(n4/3), or even O(n(log n)2) time. You can read about a method for performing the shell sort in O(n(log n)2) time on Robert Sedgewick’s page at Princeton: 3
  • 7. http://www.cs.princeton.edu/~rs/shell/paperF.pdf Note that you can find complete C code for the shell sort at the beginning of the paper, so please wait until you have finished this exercise to read it. (a) First, we will modify the shift element() function from the insertion sort code for the shell sort. The new function, started for you below, should start at index i and shift by intervals of size gap. Write the new function, using array indexing or pointers. Assume that i ≥ gap. void shift element by gap ( unsigned int i , unsigned int gap ) { /∗ i n s e r t code here ∗/ } Answer: One possible version of this function is: void shift element by gap ( unsigned int i , unsigned int gap ) { int ivalue ; unsigned int j ; for ( j = i , iva lue = arr [ i ] ; j >= gap && arr [ j−gap ] > ivalue ; j −= gap ) arr [ j ] = arr [ j−gap ] ; move/∗ ∗/ arr [ j ] = iva lue ; /∗ i n s e r t element ∗/ } (b) Now, we need to write the main shell sort routine. Using the template below, fill in the missing code so that the insertion sort in the inner loop compares every element to the element gap spaces before it. void s h e l l unsigned s o r t ( void ) { int gap , i , len = array length ( arr ) ; /∗ sort , comparing against f a r t h e r away elements f i r s t , then c l o s e r elements ∗/ for ( gap = len /2; gap > 0; gap /= 2) { /∗ do i n s e rt i o n −l i k e sort , but comparing and s h i f t i n g elements in multiples of gap ∗/ for ( /∗ i n s e r t code here ∗/ ) { i f ( /∗ i n s e r t code here ∗/ ) { /∗ out of order , do s h i f t ∗/ shift element by gap ( i , gap ) ; } } } } Answer: One possible solution is shown below: void s h e l l unsigned s o r t ( void ) { int gap , i , len = array length ( arr ) ; /∗ compare against f a r t h e r away elements f i r s t , then c l o s e r elements ∗/ for ( gap = len /2; gap > 0; gap /= 2) /∗ comparing and s h i f t i n g elements { in multiples of gap ∗/ for ( i = gap ; i < len ; i++) i f ( arr [ i−gap ] > arr [ i ] ) shift element by gap ( i , gap ) ; /∗ out of order , do s h i f t ∗/ } } 4