SlideShare a Scribd company logo
/**
* 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

PDF
삼성 바다 앱개발 실패 노하우 2부
PPTX
Introduction to Javascript
PDF
OOP in Rust
PDF
Demystifying Prototypes
PDF
JavaScript: enter the dragon
PPT
Operator Overloading
PDF
Python 如何執行
PPTX
Memory management in cocos2d x - Le Duy Vu
삼성 바다 앱개발 실패 노하우 2부
Introduction to Javascript
OOP in Rust
Demystifying Prototypes
JavaScript: enter the dragon
Operator Overloading
Python 如何執行
Memory management in cocos2d x - Le Duy Vu

What's hot (20)

PPTX
Queue oop
PDF
String searching
PPTX
Lrz kurse: r as superglue
PPTX
C++ idioms.pptx
PPTX
MFC Message Handling
PPT
Object oriented programming; operator overloading array
DOCX
Miblagh (2)
PPT
PDF
Ragel talk
ODP
Functors, applicatives, monads
PDF
2018 cosup-delete unused python code safely - english
DOCX
Arrry structure Stacks in data structure
PPT
Memory Management In C++
PDF
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
DOCX
2 a networkflow
PDF
Easily mockingdependenciesinc++ 2
RTF
PPTX
Stack using Linked List
Queue oop
String searching
Lrz kurse: r as superglue
C++ idioms.pptx
MFC Message Handling
Object oriented programming; operator overloading array
Miblagh (2)
Ragel talk
Functors, applicatives, monads
2018 cosup-delete unused python code safely - english
Arrry structure Stacks in data structure
Memory Management In C++
A "Do-It-Yourself" Specification Language with BeepBeep 3 (Talk @ Dagstuhl 2017)
2 a networkflow
Easily mockingdependenciesinc++ 2
Stack using Linked List
Ad

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

PDF
PDF
888678123317368915236 #include stdio.h #include stdlib.h.pdf
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
PDF
Make the table for program in this project. Do not use software .pdf
PPT
COMPUTER PROGRAMMING UNIT 1 Lecture 5
PDF
Program to sort the n names in an alphabetical order
DOCX
Write a complete C programme to implement the following sorting algor.docx
DOCX
Bubble sorting lab manual
ODP
Bubble sort
PPTX
bubble sorting of an array in 8086 assembly language
PPT
Sorting algorithums > Data Structures & Algorithums
PDF
CP PPT_Unit IV computer programming in c.pdf
PDF
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
PPT
DOCX
ADA FILE
PPT
PPTX
Data structure presentation sorting
DOCX
888678123317368915236 #include stdio.h #include stdlib.h.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
Make the table for program in this project. Do not use software .pdf
COMPUTER PROGRAMMING UNIT 1 Lecture 5
Program to sort the n names in an alphabetical order
Write a complete C programme to implement the following sorting algor.docx
Bubble sorting lab manual
Bubble sort
bubble sorting of an array in 8086 assembly language
Sorting algorithums > Data Structures & Algorithums
CP PPT_Unit IV computer programming in c.pdf
465659705-15-DSA-PPT-Sorting-Techniques-I.pdf
ADA FILE
Data structure presentation sorting
Ad

More from Sumedha (20)

PPTX
Deutsch famous facts
PPTX
German music
PDF
Ada scan
PDF
Scan2
PDF
Scan1
PPT
998e6 des
PPT
Selab1 slides
DOCX
Reading skills
DOC
Negotiations
DOC
Rapport
DOC
Meeetings
DOCX
Group discussions
DOC
Small talk
PPT
Dc8c4f010f40.hanoi.towers
PPT
Is sort andy-le
PPT
Merge sort
PPT
Group discussion
DOCX
Group discussions eng project
PDF
Eng presentation
PDF
Computer architecture kai hwang
Deutsch famous facts
German music
Ada scan
Scan2
Scan1
998e6 des
Selab1 slides
Reading skills
Negotiations
Rapport
Meeetings
Group discussions
Small talk
Dc8c4f010f40.hanoi.towers
Is sort andy-le
Merge sort
Group discussion
Group discussions eng project
Eng presentation
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; }