SlideShare a Scribd company logo
1 of 3
Write a C program that implements a simple array-based insertion sort. This problem is like
problem 3 below but much simpler (and is therefore a good starting point for solving problem 3).
Your program must read in the integers from the accompanying data file and use insertion sort to
store the sorted data into an array. If you must insert an element between two existing values,
then you must also move (or shift) the elements all elements with an index > = the index where
you wish to insert the new element. Note that you can find the insertion sort algorithm in the
textbook and the slides. ADDITIONAL REQUIREMENT: Your program must output the total
notequalto of assignments performed in your sorting function from insertion and array element
shifting (don't worry about assignments like control variable updates in loops, etc). This
notequalto will be used in problem notequalto 4 below. Include this notequalto in the written
document portion of your assignment submission. It can be something simple like: notequalto of
sorting assignments =
Solution
#include <stdio.h>
int main() {
int a[10], i, j, k;
FILE *fp;
fp = fopen("f://numbers.txt", "r");
//assuming file contains 10 numbers;
for (i = 0; i < 10; i++) {
fscanf(fp, "%d", &a[i]);
}
fclose(fp);
printf("unsorted elements are: ");
for (i = 0; i < 10; i++) {
printf("%dt", a[i]);
}
//sorting
for (j = 1; j < 10; j++) {
k = a[j];
i = j - 1;
//shifting
while (i >= 0 && k < a[i]) {
a[i+1]=a[i];
i = i - 1;
}
//insertion
a[i + 1] = k;
}
printf(" sorted elements are: ");
for (i = 0; i < 10; i++) {
printf("%dt", a[i]);
}
return 0;
}
output:
unsorted elements are: 23 45 12 11 6 89 24 67 9 37
sorted elements are: 6 9 11 12 23 24 37 45 67 89
RUN SUCCESSFUL (total time: 523ms)
numbers.txt
23
45
12
11
6
89
24
67
9
37
Write a C program that implements a simple array-based insertion sort-.docx

More Related Content

Similar to Write a C program that implements a simple array-based insertion sort-.docx

E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docxshandicollingwood
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in cfloraaluoch3
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfezhilvizhiyan
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Salman Qamar
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxChandrakantDivate1
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)SURBHI SAROHA
 
Function in c program
Function in c programFunction in c program
Function in c programumesh patil
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageJenish Bhavsar
 
C intro
C introC intro
C introKamran
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPTTheVerse1
 
[計一] Basic r programming final0918
[計一] Basic r programming   final0918[計一] Basic r programming   final0918
[計一] Basic r programming final0918Yen_CY
 
[計一] Basic r programming final0918
[計一] Basic r programming   final0918[計一] Basic r programming   final0918
[計一] Basic r programming final0918Chia-Yi Yen
 

Similar to Write a C program that implements a simple array-based insertion sort-.docx (20)

E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcements.docx
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
 
Functions
FunctionsFunctions
Functions
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02Ds12 140715025807-phpapp02
Ds12 140715025807-phpapp02
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Functions
Functions Functions
Functions
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Storage classes arrays & functions in C Language
Storage classes arrays & functions in C LanguageStorage classes arrays & functions in C Language
Storage classes arrays & functions in C Language
 
C intro
C introC intro
C intro
 
data_structure (1).pptx
data_structure (1).pptxdata_structure (1).pptx
data_structure (1).pptx
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT9. DBMS Experiment Laboratory PresentationPPT
9. DBMS Experiment Laboratory PresentationPPT
 
[計一] Basic r programming final0918
[計一] Basic r programming   final0918[計一] Basic r programming   final0918
[計一] Basic r programming final0918
 
[計一] Basic r programming final0918
[計一] Basic r programming   final0918[計一] Basic r programming   final0918
[計一] Basic r programming final0918
 

More from SUKHI5

Ghocse the correct answer below- A- No- because the probabafy of burvi.docx
Ghocse the correct answer below- A- No- because the probabafy of burvi.docxGhocse the correct answer below- A- No- because the probabafy of burvi.docx
Ghocse the correct answer below- A- No- because the probabafy of burvi.docxSUKHI5
 
Given Data- MAGLIOCHETTI MOVING CORP- Statement of Earni.docx
Given Data-               MAGLIOCHETTI MOVING CORP- Statement of Earni.docxGiven Data-               MAGLIOCHETTI MOVING CORP- Statement of Earni.docx
Given Data- MAGLIOCHETTI MOVING CORP- Statement of Earni.docxSUKHI5
 
From this table- which mineral level had a significance on which varia.docx
From this table- which mineral level had a significance on which varia.docxFrom this table- which mineral level had a significance on which varia.docx
From this table- which mineral level had a significance on which varia.docxSUKHI5
 
Given a ListItem class- complete main() using the built-in list type t.docx
Given a ListItem class- complete main() using the built-in list type t.docxGiven a ListItem class- complete main() using the built-in list type t.docx
Given a ListItem class- complete main() using the built-in list type t.docxSUKHI5
 
Given a -discrete object- O- the notationO means what- Choose all the.docx
Given a -discrete object- O- the notationO means what- Choose all the.docxGiven a -discrete object- O- the notationO means what- Choose all the.docx
Given a -discrete object- O- the notationO means what- Choose all the.docxSUKHI5
 
Give five examples of unsustainable living or actions from your own ev.docx
Give five examples of unsustainable living or actions from your own ev.docxGive five examples of unsustainable living or actions from your own ev.docx
Give five examples of unsustainable living or actions from your own ev.docxSUKHI5
 
Give an inductive definition of the function rev which takes a list an.docx
Give an inductive definition of the function rev which takes a list an.docxGive an inductive definition of the function rev which takes a list an.docx
Give an inductive definition of the function rev which takes a list an.docxSUKHI5
 
Give feedback19 lines (16 sloc) 450 Bytes.docx
Give feedback19 lines (16 sloc) 450 Bytes.docxGive feedback19 lines (16 sloc) 450 Bytes.docx
Give feedback19 lines (16 sloc) 450 Bytes.docxSUKHI5
 
Give a big-Oh characterization- in terms of n- of the running time of.docx
Give a big-Oh characterization- in terms of n- of the running time of.docxGive a big-Oh characterization- in terms of n- of the running time of.docx
Give a big-Oh characterization- in terms of n- of the running time of.docxSUKHI5
 
Give at least 2 reasons why an entire population of microbes does not.docx
Give at least 2 reasons why an entire population of microbes does not.docxGive at least 2 reasons why an entire population of microbes does not.docx
Give at least 2 reasons why an entire population of microbes does not.docxSUKHI5
 
Giovani Photography bills $650 to Diane Fenning for photography servic.docx
Giovani Photography bills $650 to Diane Fenning for photography servic.docxGiovani Photography bills $650 to Diane Fenning for photography servic.docx
Giovani Photography bills $650 to Diane Fenning for photography servic.docxSUKHI5
 
Germ Theory was a framework that--- All of these argued that disease w.docx
Germ Theory was a framework that--- All of these argued that disease w.docxGerm Theory was a framework that--- All of these argued that disease w.docx
Germ Theory was a framework that--- All of these argued that disease w.docxSUKHI5
 
General Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docx
General Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docxGeneral Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docx
General Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docxSUKHI5
 
Gereldo recently won a lottery and chose to receive $70-000 todsy inst.docx
Gereldo recently won a lottery and chose to receive $70-000 todsy inst.docxGereldo recently won a lottery and chose to receive $70-000 todsy inst.docx
Gereldo recently won a lottery and chose to receive $70-000 todsy inst.docxSUKHI5
 
genomes are usually circular loops of double-stranded deoxyribonucleic.docx
genomes are usually circular loops of double-stranded deoxyribonucleic.docxgenomes are usually circular loops of double-stranded deoxyribonucleic.docx
genomes are usually circular loops of double-stranded deoxyribonucleic.docxSUKHI5
 
Gastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docx
Gastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docxGastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docx
Gastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docxSUKHI5
 
Geert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docx
Geert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docxGeert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docx
Geert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docxSUKHI5
 
Gale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docx
Gale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docxGale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docx
Gale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docxSUKHI5
 
Future valuesFor the case shown in the following table- calculate the.docx
Future valuesFor the case shown in the following table- calculate the.docxFuture valuesFor the case shown in the following table- calculate the.docx
Future valuesFor the case shown in the following table- calculate the.docxSUKHI5
 
Function Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docx
Function Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docxFunction Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docx
Function Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docxSUKHI5
 

More from SUKHI5 (20)

Ghocse the correct answer below- A- No- because the probabafy of burvi.docx
Ghocse the correct answer below- A- No- because the probabafy of burvi.docxGhocse the correct answer below- A- No- because the probabafy of burvi.docx
Ghocse the correct answer below- A- No- because the probabafy of burvi.docx
 
Given Data- MAGLIOCHETTI MOVING CORP- Statement of Earni.docx
Given Data-               MAGLIOCHETTI MOVING CORP- Statement of Earni.docxGiven Data-               MAGLIOCHETTI MOVING CORP- Statement of Earni.docx
Given Data- MAGLIOCHETTI MOVING CORP- Statement of Earni.docx
 
From this table- which mineral level had a significance on which varia.docx
From this table- which mineral level had a significance on which varia.docxFrom this table- which mineral level had a significance on which varia.docx
From this table- which mineral level had a significance on which varia.docx
 
Given a ListItem class- complete main() using the built-in list type t.docx
Given a ListItem class- complete main() using the built-in list type t.docxGiven a ListItem class- complete main() using the built-in list type t.docx
Given a ListItem class- complete main() using the built-in list type t.docx
 
Given a -discrete object- O- the notationO means what- Choose all the.docx
Given a -discrete object- O- the notationO means what- Choose all the.docxGiven a -discrete object- O- the notationO means what- Choose all the.docx
Given a -discrete object- O- the notationO means what- Choose all the.docx
 
Give five examples of unsustainable living or actions from your own ev.docx
Give five examples of unsustainable living or actions from your own ev.docxGive five examples of unsustainable living or actions from your own ev.docx
Give five examples of unsustainable living or actions from your own ev.docx
 
Give an inductive definition of the function rev which takes a list an.docx
Give an inductive definition of the function rev which takes a list an.docxGive an inductive definition of the function rev which takes a list an.docx
Give an inductive definition of the function rev which takes a list an.docx
 
Give feedback19 lines (16 sloc) 450 Bytes.docx
Give feedback19 lines (16 sloc) 450 Bytes.docxGive feedback19 lines (16 sloc) 450 Bytes.docx
Give feedback19 lines (16 sloc) 450 Bytes.docx
 
Give a big-Oh characterization- in terms of n- of the running time of.docx
Give a big-Oh characterization- in terms of n- of the running time of.docxGive a big-Oh characterization- in terms of n- of the running time of.docx
Give a big-Oh characterization- in terms of n- of the running time of.docx
 
Give at least 2 reasons why an entire population of microbes does not.docx
Give at least 2 reasons why an entire population of microbes does not.docxGive at least 2 reasons why an entire population of microbes does not.docx
Give at least 2 reasons why an entire population of microbes does not.docx
 
Giovani Photography bills $650 to Diane Fenning for photography servic.docx
Giovani Photography bills $650 to Diane Fenning for photography servic.docxGiovani Photography bills $650 to Diane Fenning for photography servic.docx
Giovani Photography bills $650 to Diane Fenning for photography servic.docx
 
Germ Theory was a framework that--- All of these argued that disease w.docx
Germ Theory was a framework that--- All of these argued that disease w.docxGerm Theory was a framework that--- All of these argued that disease w.docx
Germ Theory was a framework that--- All of these argued that disease w.docx
 
General Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docx
General Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docxGeneral Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docx
General Knowledge- In 1927 The Jazz Singer- starring Al Jolson- was hi.docx
 
Gereldo recently won a lottery and chose to receive $70-000 todsy inst.docx
Gereldo recently won a lottery and chose to receive $70-000 todsy inst.docxGereldo recently won a lottery and chose to receive $70-000 todsy inst.docx
Gereldo recently won a lottery and chose to receive $70-000 todsy inst.docx
 
genomes are usually circular loops of double-stranded deoxyribonucleic.docx
genomes are usually circular loops of double-stranded deoxyribonucleic.docxgenomes are usually circular loops of double-stranded deoxyribonucleic.docx
genomes are usually circular loops of double-stranded deoxyribonucleic.docx
 
Gastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docx
Gastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docxGastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docx
Gastrointestinal Disorders- Teaching about Increasing Fiber Intake- pl.docx
 
Geert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docx
Geert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docxGeert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docx
Geert Hofstede surveyed more than 116-000 IBM employees in 40 countrie.docx
 
Gale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docx
Gale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docxGale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docx
Gale- Mctean- and Lux are partners of Burgers and Brew Company with ca.docx
 
Future valuesFor the case shown in the following table- calculate the.docx
Future valuesFor the case shown in the following table- calculate the.docxFuture valuesFor the case shown in the following table- calculate the.docx
Future valuesFor the case shown in the following table- calculate the.docx
 
Function Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docx
Function Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docxFunction Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docx
Function Name- isOneCapital Input- 1- (char) A letter 2- (char) A lett.docx
 

Recently uploaded

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use CasesTechSoup
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonhttgc7rh9c
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...EADTU
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 

Recently uploaded (20)

Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Introduction to TechSoup’s Digital Marketing Services and Use Cases
Introduction to TechSoup’s Digital Marketing  Services and Use CasesIntroduction to TechSoup’s Digital Marketing  Services and Use Cases
Introduction to TechSoup’s Digital Marketing Services and Use Cases
 
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lessonQUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
QUATER-1-PE-HEALTH-LC2- this is just a sample of unpacked lesson
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 

Write a C program that implements a simple array-based insertion sort-.docx

  • 1. Write a C program that implements a simple array-based insertion sort. This problem is like problem 3 below but much simpler (and is therefore a good starting point for solving problem 3). Your program must read in the integers from the accompanying data file and use insertion sort to store the sorted data into an array. If you must insert an element between two existing values, then you must also move (or shift) the elements all elements with an index > = the index where you wish to insert the new element. Note that you can find the insertion sort algorithm in the textbook and the slides. ADDITIONAL REQUIREMENT: Your program must output the total notequalto of assignments performed in your sorting function from insertion and array element shifting (don't worry about assignments like control variable updates in loops, etc). This notequalto will be used in problem notequalto 4 below. Include this notequalto in the written document portion of your assignment submission. It can be something simple like: notequalto of sorting assignments = Solution #include <stdio.h> int main() { int a[10], i, j, k; FILE *fp; fp = fopen("f://numbers.txt", "r"); //assuming file contains 10 numbers; for (i = 0; i < 10; i++) { fscanf(fp, "%d", &a[i]); } fclose(fp); printf("unsorted elements are: "); for (i = 0; i < 10; i++) { printf("%dt", a[i]); } //sorting for (j = 1; j < 10; j++) { k = a[j]; i = j - 1; //shifting
  • 2. while (i >= 0 && k < a[i]) { a[i+1]=a[i]; i = i - 1; } //insertion a[i + 1] = k; } printf(" sorted elements are: "); for (i = 0; i < 10; i++) { printf("%dt", a[i]); } return 0; } output: unsorted elements are: 23 45 12 11 6 89 24 67 9 37 sorted elements are: 6 9 11 12 23 24 37 45 67 89 RUN SUCCESSFUL (total time: 523ms) numbers.txt 23 45 12 11 6 89 24 67 9 37