SlideShare a Scribd company logo
1 of 1
/**
* An example of bubble sort written in C.
* Author: Nebu Pookins (nebu@gta.igs.net)
*
* I've tried to keep the "logic" of this program as close to the
* QuickBasic example as possible.
*/
int main(void) {
const int numberOfElements = 6;
char arrayOfCharsToSort[numberOfElements];
//Fill in the array with the unsorted data.
arrayOfCharsToSort[0] = 'C';
arrayOfCharsToSort[1] = 'A';
arrayOfCharsToSort[2] = 'Z';
arrayOfCharsToSort[3] = 'G';
arrayOfCharsToSort[4] = 'S';
arrayOfCharsToSort[5] = 'J';
//Start sorting
int i, j; //variables used for for loops.
char temporaryCharHolder; //used to swap values of two array entries
for (i = 0; i < numberOfElements; i++) {
for (j = 0; j < numberOfElements - 1; j++) {
if (arrayOfCharsToSort[j] > arrayOfCharsToSort[j + 1]) {
temporaryCharHolder = arrayOfCharsToSort[j];
arrayOfCharsToSort[j] = arrayOfCharsToSort[j + 1];
arrayOfCharsToSort[j + 1] = temporaryCharHolder;
}
}
}
//Print out the sorted results
for (i = 0; i < numberOfElements; i++) {
printf("%dn", arrayOfCharsToSort[i]);
}
return 0;
}

More Related Content

What's hot

What's hot (20)

Queue oop
Queue   oopQueue   oop
Queue oop
 
String searching
String searchingString searching
String searching
 
Php 5.6
Php 5.6Php 5.6
Php 5.6
 
Lrz kurse: r as superglue
Lrz kurse: r as superglueLrz kurse: r as superglue
Lrz kurse: r as superglue
 
C++ idioms.pptx
C++ idioms.pptxC++ idioms.pptx
C++ idioms.pptx
 
MFC Message Handling
MFC Message HandlingMFC Message Handling
MFC Message Handling
 
Object oriented programming; operator overloading array
Object oriented programming; operator overloading arrayObject oriented programming; operator overloading array
Object oriented programming; operator overloading array
 
Miblagh (2)
Miblagh (2)Miblagh (2)
Miblagh (2)
 
C++ tutorial
C++ tutorialC++ tutorial
C++ tutorial
 
Cpl
CplCpl
Cpl
 
Ragel talk
Ragel talkRagel talk
Ragel talk
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english
 
Arrry structure Stacks in data structure
Arrry structure Stacks  in data structureArrry structure Stacks  in data structure
Arrry structure Stacks in data structure
 
Memory Management In C++
Memory Management In C++Memory Management In C++
Memory Management In C++
 
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
 
2 a networkflow
2 a networkflow2 a networkflow
2 a networkflow
 
Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2Easily mockingdependenciesinc++ 2
Easily mockingdependenciesinc++ 2
 
Sorter
SorterSorter
Sorter
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 

Similar to An example of bubble sort written in c

Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfbadshetoms
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationNaveen Gupta
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfwasemanivytreenrco51
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...GkhanGirgin3
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxVeerannaKotagi1
 
C programming assignment help
C programming assignment helpC programming assignment help
C programming assignment helpHebrew Johnson
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questionsSrikanth
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3Srikanth
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docxadkinspaige22
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more steviesellars
 

Similar to An example of bubble sort written in c (20)

Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
Arrays
ArraysArrays
Arrays
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
 
c programming
c programmingc programming
c programming
 
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
booksoncprogramminglanguage-anintroductiontobeginnersbyarunumrao4-21101016591...
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
C programming assignment help
C programming assignment helpC programming assignment help
C programming assignment help
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays
ArraysArrays
Arrays
 
C aptitude questions
C aptitude questionsC aptitude questions
C aptitude questions
 
C - aptitude3
C - aptitude3C - aptitude3
C - aptitude3
 
C++11 - STL Additions
C++11 - STL AdditionsC++11 - STL Additions
C++11 - STL Additions
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
@author Derek Harter @cwid 123 45 678 @class .docx
@author Derek Harter  @cwid   123 45 678  @class  .docx@author Derek Harter  @cwid   123 45 678  @class  .docx
@author Derek Harter @cwid 123 45 678 @class .docx
 
Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more Use a simple vector you created before to create two other more
Use a simple vector you created before to create two other more
 
pointers
pointerspointers
pointers
 

More from Sumedha

Deutsch famous facts
Deutsch famous factsDeutsch famous facts
Deutsch famous factsSumedha
 
German music
German musicGerman music
German musicSumedha
 
Selab1 slides
Selab1 slidesSelab1 slides
Selab1 slidesSumedha
 
Reading skills
Reading skillsReading skills
Reading skillsSumedha
 
Negotiations
NegotiationsNegotiations
NegotiationsSumedha
 
Group discussions
Group discussionsGroup discussions
Group discussionsSumedha
 
Small talk
Small talkSmall talk
Small talkSumedha
 
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towersDc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towersSumedha
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-leSumedha
 
Merge sort
Merge sortMerge sort
Merge sortSumedha
 
Group discussion
Group discussionGroup discussion
Group discussionSumedha
 
Group discussions eng project
Group discussions eng projectGroup discussions eng project
Group discussions eng projectSumedha
 
Eng presentation
Eng presentationEng presentation
Eng presentationSumedha
 
Computer architecture kai hwang
Computer architecture   kai hwangComputer architecture   kai hwang
Computer architecture kai hwangSumedha
 

More from Sumedha (20)

Deutsch famous facts
Deutsch famous factsDeutsch famous facts
Deutsch famous facts
 
German music
German musicGerman music
German music
 
Ada scan
Ada scanAda scan
Ada scan
 
Scan2
Scan2Scan2
Scan2
 
Scan1
Scan1Scan1
Scan1
 
998e6 des
998e6 des998e6 des
998e6 des
 
Selab1 slides
Selab1 slidesSelab1 slides
Selab1 slides
 
Reading skills
Reading skillsReading skills
Reading skills
 
Negotiations
NegotiationsNegotiations
Negotiations
 
Rapport
RapportRapport
Rapport
 
Meeetings
MeeetingsMeeetings
Meeetings
 
Group discussions
Group discussionsGroup discussions
Group discussions
 
Small talk
Small talkSmall talk
Small talk
 
Dc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towersDc8c4f010f40.hanoi.towers
Dc8c4f010f40.hanoi.towers
 
Is sort andy-le
Is sort andy-leIs sort andy-le
Is sort andy-le
 
Merge sort
Merge sortMerge sort
Merge sort
 
Group discussion
Group discussionGroup discussion
Group discussion
 
Group discussions eng project
Group discussions eng projectGroup discussions eng project
Group discussions eng project
 
Eng presentation
Eng presentationEng presentation
Eng presentation
 
Computer architecture kai hwang
Computer architecture   kai hwangComputer architecture   kai hwang
Computer architecture kai hwang
 

An example of bubble sort written in c

  • 1. /** * An example of bubble sort written in C. * Author: Nebu Pookins (nebu@gta.igs.net) * * I've tried to keep the "logic" of this program as close to the * QuickBasic example as possible. */ int main(void) { const int numberOfElements = 6; char arrayOfCharsToSort[numberOfElements]; //Fill in the array with the unsorted data. arrayOfCharsToSort[0] = 'C'; arrayOfCharsToSort[1] = 'A'; arrayOfCharsToSort[2] = 'Z'; arrayOfCharsToSort[3] = 'G'; arrayOfCharsToSort[4] = 'S'; arrayOfCharsToSort[5] = 'J'; //Start sorting int i, j; //variables used for for loops. char temporaryCharHolder; //used to swap values of two array entries for (i = 0; i < numberOfElements; i++) { for (j = 0; j < numberOfElements - 1; j++) { if (arrayOfCharsToSort[j] > arrayOfCharsToSort[j + 1]) { temporaryCharHolder = arrayOfCharsToSort[j]; arrayOfCharsToSort[j] = arrayOfCharsToSort[j + 1]; arrayOfCharsToSort[j + 1] = temporaryCharHolder; } } } //Print out the sorted results for (i = 0; i < numberOfElements; i++) { printf("%dn", arrayOfCharsToSort[i]); } return 0; }